为什么BootstrapValidator不起作用?

时间:2014-10-04 07:32:54

标签: javascript jquery twitter-bootstrap jqbootstrapvalidation

我是网络开发的新手,在这里我知道问题非常小,但即使在两天后我也无法找到它。你能不能看一下这段代码并指出我的错误

我的代码ID here @ JSFiddle 感谢

HTML

<form id="registerForm" action="\register" method="post">
    <div class="form-group">
        <input class="form-control" type="text" name="email" placeholder="Email">
    </div>
    <div class="form-group">
        <input class="form-control" type="password" name="pass1" placeholder="Password">
    </div>
    <div class="form-group">
        <input class="form-control" type="password" name="pass2" placeholder="Confirm Password">
    </div>
    <button class="btn btn-primary" type="submit">Signup</button>
</form>

JS

$(document).ready(function () {
    $('.registerForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            email: {
                validators: {
                    notEmpty: {
                        message: 'The email is required and cannot be empty'
                    },
                    emailAddress: {
                        message: 'The input is not a valid email address'
                    }
                }
            },
            pass1: {
                validators: {
                    identical: {
                        field: 'pass2',
                        message: 'The password and its confirm are not the same'
                    }
                }
            },
            pass2: {
                validators: {
                    identical: {
                        field: 'pass1',
                        message: 'The password and its confirm are not the same'
                    }
                }
            }
        }

    });
});

3 个答案:

答案 0 :(得分:3)

要通过id获取节点:$('#registerForm')。如果您到$('.registerForm'),JS将在registerForm标记中查找课程<form>

阅读jQuery API是防止这样的错误的好方法。 http://api.jquery.com/category/selectors/

答案 1 :(得分:0)

您的JavaScript绑定到类registerForm,而不是id。在JavaScript代码中将.registerForm更改为#registerForm。

编辑: 或者将class =“registerForm”添加到html中的YouTube表单元素

答案 2 :(得分:0)

在HTML格式中分配了一个id="registerForm",而在js文件中,您正在针对类$('.contactForm')调用一个函数,只需将$('.contactForm')更改为$('#contactForm')即可解决你的问题。