【问题标题】:JQuery form validation questionjQuery表单验证问题
【发布时间】:2011-07-17 16:25:40
【问题描述】:
我有一个搜索表单,其中包含大约十个字段。其中几个是“文本”类型的输入元素,其中几个是选择框。用户应选择至少一个条件进行搜索。我可以为所有 10 个字段编写类似“if input1!=null && input2!= null...”的内容,以检查用户是否选择了至少一个标准。
但我觉得这是很多代码。有没有我可以使用 jquery 编写一行代码来满足这个要求(即用户应该至少选择一个搜索条件)?
【问题讨论】:
标签:
jquery
jquery-selectors
jquery-validate
【解决方案1】:
$('input[value!=""]').length
这显示了其中包含文本的输入字段的数量(不同于“”)。 Try it here
【解决方案3】:
$('input, select').each(function(){
var current = $(this);
if( current.val() == //yourcriteria// ) {
//do stuff!
}
});
【解决方案4】:
类似
var somethingSelectedInEach = true;
$("select").each(function(i, el){
if($(this).val() == null){
somethingSelectedInEach = false;
return false;
}
});
if(somethingSelectedInEach){
alert("Yaay! You selected something in each select!");
} else {
alert("Booo. You forgot to select something in each select!");
}