我在使用jQuery的Validate插件验证我正在处理的网站上的选择框时遇到了问题。
有谁知道正确的语法?查看文档,他们似乎在选择输入上使用内联HTML5“必需”标记,但我找不到任何直接相关的javascript。
到目前为止我尝试的是:
$('#quick-search').validate({
vehicletype: {
required: true
}
});
...则...
$('#quick-search').validate({
"vehicletype[]":"required"
});
似乎没有发生任何事情,我甚至注意到我的表单元素上出现了“无效”,无缘无故!
提前致谢, 格雷厄姆
答案 0 :(得分:1)
你一定做错了语法。
因为你没有提供HTML
<form action="/smudev/CourseCategories/add" autocomplete="off" id="CourseCategoryAddForm" method="post" accept-charset="utf-8" novalidate="novalidate">
<!--Div structure-->
<section class="AddNewForm">
<section class="formField fullwidth">
<label>
Department:
<span class="starRed"> *</span>
</label>
<section class="inputOuter width160">
<div class="input select"><select name="data[CourseCategory][department_id]" id="CourseCategoryDepartmentId" class="errorjQuery">
<option value="">(choose one)</option>
<option value="4">Arts</option>
<option value="5">Commerce</option>
<option value="1">Science</option>
<option value="3">Sports</option>
</select></div>
</section>
</section>
<section class="formField formBTN">
<div class="submit"><input name="submit" class="grayBTN" title="" alt="Submit" type="submit" value="Save"></div><button type="button" onclick="redirectFun()" class="grayBTN">Cancel</button>
</section>
</section></form>
的jQuery
jQuery(document).ready(function(){
// validate add Category form when it is submitted
jQuery("#CourseCategoryAddForm").validate({
errorElement: 'div',
errorClass:'errorjQuery',
rules: {
'data[CourseCategory][department_id]': {
required: true
}
},
messages: {
'data[CourseCategory][department_id]': {
required: "Please select department."
}
},
errorPlacement: function(error, element) {
error.appendTo( element.parent());
//What to do if error occurs
$(".error-message").remove();
},
submitHandler: function(form) {
jQuery('input[type=submit]', form).attr('disabled', 'disabled');
idd = form.attr("id");
form.submit();
}
});
// validate edit Category form when it is submitted
});