我正在使用jquery验证插件,最近我不得不在我的表单中添加多选下拉列表。我使用了davidstutz bootstrap multiselect插件。但是,无法验证下面的多选框是我的html代码,我提交从多选框中选择一个值而不提供其他表单输入,即使表单已提交,
<select name="franchisee_course" class="multiselect" multiple="multiple" id="franchisee_course" size="1" placeholder="Courses">
<?php
foreach($courses as $course) {
echo "<option value=".$course['course_id']." >".$course['course_description']."</option>";
}
?>
</select>
和我的呼叫多选,
$('.multiselect').multiselect({
noneSelectedText: 'Select Courses',
includeSelectAllOption: true,
enableFiltering: true,
filterPlaceholder: 'Search course'
});
这些是多选的验证码,
$.validator.addMethod("needsSelection", function(value, element) {
return $(element).multiselect("getChecked").length > 0;
});
$.validator.messages.needsSelection = 'Select Atleast One Course';
franchisee_course: {
needsSelection: true,
required:true
},
ignore: ':hidden:not("#franchisee_course")', // I don't know what this line does
答案 0 :(得分:3)
问题是“getChecked”参数不再执行任何操作。以下是验证方法
的更新工作版本$.validator.addMethod("needsSelection", function (value, element) {
var count = $(element).find('option:selected').length;
return count > 0;
});