我使用jQuery Validate插件检查网站上的表单。这是我的HTML表单,用于检查
<form id ="formCharacteristics" align="center">
<dl>
<dt style="text-align:left; margin-left:120px;"><label for="cpuname">CPU name</label></dt>
<dd><input type="text" id="cpuname" placeholder="Intel 8086"></dd>
<div align="right" style="margin-right:30px">
<button id="buttonSubmitCharacteristics" type="button" onclick = "AddCPU()" >Done</button>
<button type="button" onclick="HideMessage()">Back</button>
</div>
</form>
这是一段JavaScript代码,它在Ajax发布之前调用验证
AddCPU(){
var form = $('#formCharacteristics');
if(!form.valid())
return;
var CPUname = $('input #cpuname');
....
}
验证在其他表单上正常工作,但在此表单上,它不会检查任何规则。我不使用相同的表格ID和输入ID 这是用于指定规则的代码
$(document).ready(function() {
$('#formCharacteristics').validate({
rules:{
cpuname: {
required: true,
maxlength: 30,
minlength: 4
}
}
});
......
)};
答案 0 :(得分:3)
您的密码:
$('#formCharacteristics').validate({
rules: {
cpuname: { // <- this is the NAME attribute
required: true,
maxlength: 30,
....
要使jQuery Validate插件正常工作,您的input
必须包含name
对象中指定的rules
...
<input type="text" name="cpuname" id="cpuname" ....