选择框上的jQuery验证不起作用 [英] jQuery validation on select box not working
本文介绍了选择框上的jQuery验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用jQuery验证插件进行客户端验证,但我的验证不适用于我的选择框。
I am using jQuery validation plugin for client side validation, but my validation does not work on my select box.
HTML
<select id="select" class="required">
<option value="-1">Choose</option>
<option value="child">test2</option>
</select>
JavaScript
$("#formid").validate({
select: {
required: function(element) {
if ($("#select").val() == '-1') {
return false;
} else {
return true;
}
}
}
});
我如何才能正常工作?
推荐答案
解决此问题的一种简单方法是给无效选项
<的值/ code>。然后只需在表单上调用validate,当
选择
被选中时,它就不会提交。
A simple way to fix this problem is to give the non valid option
the value of ""
. Then simply call validate on your form and it will not submit when "Choose"
is selected.
HTML
<form id="formid">
<select name="select" class="required">
<option value="">Choose</option>
<option value="child">test2</option>
</select>
<input type="submit" />
</form>
JavaScript
$("#formid").validate();
Demo
这篇关于选择框上的jQuery验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文