我正在使用 Bootstrap 验证器来确保在提交表单时填写所有字段。但是我的验证器没有按预期工作!
这是 JSFiddle:http: //jsfiddle.net/3v3011e0/
如果您使用它并提交所有字段为空的表单,并将其中一个更改为非空,则所有字段都变为绿色。这不应该发生。只有正常工作的字段才应该是绿色的。如图所示,用户名和密码为空,但显示为绿色。此外,该图标仅显示用户名。我该如何解决?
HTML
<form action="#" method="POST" id="myForm">
<div class="form-group">
<input id="userField" type="text" name="username" placeholder="Enter user name" class="form-control" />
<input id="password" type="password" name="password" placeholder="Choose a password" class="form-control" />
<select id="myPicker" name="num" class="selectpicker form-control">
<option value="" selected="selected">Select number</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary">submit</button>
</div>
</form>
Javascript
$(document).ready(function() {
$('#myForm').bootstrapValidator({
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
username: {
validators: {
notEmpty: {
message: 'The username is required and cannot be empty'
}
}
},
password: {
validators: {
notEmpty: {
message: 'The password is required and cannot be empty'
}
}
},
num: {
validators: {
notEmpty: {
message: 'Number is required and cannot be empty'
}
}
}
}
});
});