我尝试使用jquery.validation插件显示错误,但我没有收到任何消息。
当我点击提交时,我会在空白字段中收到无效警报,但在摘要div中没有消息。谁能告诉我我错过了什么?
谢谢
<div class="form-group form-group-sm">
<label class="control-label requiredField" for="SW">
Social Worker
<span class="asteriskField">
*
</span>
</label>
<input class="form-control" id="SW" name="SW" type="text" required="required"/>
</div>
<div class="form-group form-group-sm">
<label class="control-label requiredField" for="NMN">
Named Nurse
<span class="asteriskField">
*
</span>
</label>
<input class="form-control" id="NMN" name="NMN" type="text" required="required"/>
</div>
<div id="summary"></div>
<div class="form-group">
<div>
<button class="btn btn-primary " name="submit" id="submit" type="submit">
Submit
</button>
<script>
$("#mform").validate({
showErrors: function(errorMap, errorList) {
$("#summary").html("Your form contains "
+ this.numberOfInvalids()
+ " errors, see details below.");
this.defaultShowErrors();
}
});
</script>
答案 0 :(得分:-1)
工作小提琴:http://jsfiddle.net/sandenay/4psggt9q/
我已添加jQuery
和jQuery Validate
个插件
您的JS代码:
$(document).ready(function(){
$("#submit").click(function(){
$("#mform").validate({
showErrors: function(errorMap, errorList) {
$("#summary").html("Your form contains "
+ this.numberOfInvalids()
+ " errors, see details below.");
this.defaultShowErrors();
}
});
});
});
HTML:
<form id="mform">
.........other html code............
// Observe that I have removed type="submit" below code
<button class="btn btn-primary " name="submit" id="submit">
Submit
</button>
</form>
defaultShowErrors()
显示input
字段以外的错误。
numberOfInvalids()
显示所有错误的计数。