我对表单验证的 Bootstrap 3 实现感到困惑。我是 Bootstrap 的新手,我似乎无法使用不包含提交按钮的表单进行表单验证。
可以使用以下代码查看我正在寻找的表单验证类型:

http://jsfiddle.net/hTPY7/1107/

<form>
    <div class="form-group">
        <label class="control-label" for="username">Email:</label>
        <div class="input-group">
            <input class="form-control" placeholder="Email" name="email" type="email" required />
        </div>
    </div>

    <div class="form-group">
        <label class="control-label" for="password">Password:</label>
        <div class="input-group">
            <input class="form-control" type="password" placeholder="Password" name="lastname" type="text" required />
        </div>
    </div>

        <button type="submit" class="btn btn-primary">Submit</button>
</form>

我知道我可以使用一些 JQuery 提交表单,如下面的代码,但我如何使用 Bootstrap 3 验证?
$( "#createUserSubmit" ).click(function() {
  $( "#newUserForm" ).submit();
});

我多年来一直在阅读 Stackoverflow,但这是我的第一篇文章。我四处寻找答案,但我能找到的只是使用其他验证库的解决方案。我想使用 Bootstrap 自己的内置函数。

谢谢!

最佳答案

我做了一些研究,现在我知道我看到的表单验证不是 Bootstrap 的功能,而是一个新的 CSS3/HTML5 功能。我认为除非提交按钮包含在表单中,否则不可能(没有JS)执行此表单验证。这是一个不起作用的例子:

http://jsfiddle.net/hTPY7/1112/

这是如何修复它的示例:

http://jsfiddle.net/hTPY7/1113/
<form> 元素不仅需要包围表单,还需要包围 Bootstrap 模态 div。

这是另一篇文章,它很好地总结了我的问题:

HTML5 form validation for AJAX button submit

10-05 20:44
查看更多