javascript-jQuery验证以选中selectbox unqiue和必需
作者:互联网
我使用了jQuery validate插件来检查选择框的唯一性和必需性.
但是,唯一检查根本不起作用,而仅第一个选择框才需要检查.
如何解决呢?谢谢
jQuery的
$("#insertResult").validate({
rules: {
'select[]': {
required: true,
unique: true
}
},
});
html
<form id="insertResult" method="post" action="excelSQL.php" >
<select id="selectField0" name="select[]" style="float:left">
<option selected="" value="">Select one</option>
<option value="Email">Email</option>
<option value="1_Name2">1_Name2</option>
</select>
<select id="selectField1" name="select[]" style="float:left">
<option selected="" value="">Select one</option>
<option value="Email">Email</option>
<option value="1_Name2">1_Name2</option>
</select>
</form>
更新:
我已经编辑了之前完成的js文件,并且尝试了selectField0或selectField_0,但还是没有运气
checkForm: function() {
this.prepareForm();
for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
if (this.findByName( elements[i].name ).length != undefined && this.findByName( elements[i].name ).length > 1) {
for (var cnt = 0; cnt < this.findByName( elements[i].name ).length; cnt++) {
this.check( this.findByName( elements[i].name )[cnt] );
}
} else {
this.check( elements[i] );
}
}
return this.valid();
},
解决方法:
这是jQuery验证程序的常见问题.有两种方法可以解决此问题:
方法1:
修改jQuery Validator插件,使其按照以下说明进行处理:http://www.codeboss.in/web-funda/2009/05/27/jquery-validation-for-array-of-input-elements/
方法2:
将验证器分别应用于每个选择:
HTML:
<form id="insertResult" method="post" action="excelSQL.php" >
<select id="selectField0" name="select[]" style="float:left" class="error">
<option selected="" value="">Select one</option>
<option value="Email">Email</option>
<option value="1_Name2">1_Name2</option>
</select>
<select id="selectField1" name="select[]" style="float:left">
<option selected="" value="">Select one</option>
<option value="Email">Email</option>
<option value="1_Name2">1_Name2</option>
</select>
...
</form>
JavaScript:
$("#insertResult").find("select").each(function() {
$(this).validate({
rules: {
'select[]': {
required: true,
unique: true
}
}
});
});
方法3:
尽管jQuery插件对于执行更基本的任务可能非常有价值,但在某些情况下,您尝试执行的操作实际上可能超出了插件设计的范围.每当我听到诸如“修改库代码”之类的解决方案时,就会使我认为也许该库不适合我.这取决于修改的复杂性,以及我的同事或后继者可能难以弄清为什么在他们将插件更新到最新版本时事情停止工作的原因.
因此,方法3完全避开了jQuery验证器,并且仅使用jQuery来促进基本验证:
JavaScript:
// submit handler for form
$('#insertResult').submit(function(event) {
// if either field is not selected, show error and don't submit
if( itemNotSelected( $('#selectField0'), $('#selectField1') ) == true ) {
$('.error2').css("display","block");
event.preventDefault();
return false;
} else if( isEqual($("#selectField0"), $("#selectField1") ) == true ) {
$('.error2').css("display","none");
$('.error1').css("display","block");alert("afda")
event.preventDefault();
return false;
}
});
// hide error text on focus of element
$('#selectField0, #selectField1').focus(function() {
$('.error2, .error1').hide();
});
// helper methods to check if both fields are equal
function isEqual(elem1, elem2) {
return (elem1.find("option:selected").html() ==
elem2.find("option:selected").html());
}
// helper methods to check if one field (or both) is not selected
function itemNotSelected(elem1, elem2) {
return ( elem1.find("option:selected").html() == "Select one" ||
elem2.find("option:selected").html() == "Select one" );
}
HTML:
<select id="selectField0" name="select[]" style="float:left" class="error">
<option selected="" value="">Select one</option>
<option value="Email">Email</option>
<option value="1_Name2">1_Name2</option>
</select>
<select id="selectField1" name="select[]" style="float:left">
<option selected="" value="">Select one</option>
<option value="Email">Email</option>
<option value="1_Name2">1_Name2</option>
</select>
<span class="error2" style="display:none">These are required fields</span>
<span class="error1" style="display:none">Fields must be unique</span>
<input type="submit" name="submit" value="submit" />
仅当满足以下条件时,以上代码才会提交表单:
>两个选择框都选择了项目.满足“必需”条件.
>选项不一样,并且满足“唯一”条件.
>当用户聚焦选择框时,错误消失.
>如果用户在不满足上述提交条件的情况下尝试提交,则会显示以下错误:
>如果未选择一个或多个选择框,则会显示“这些是必填字段”消息.
>如果两者相等并被选择,则显示“字段必须唯一”消息.
标签:jquery-validate,javascript,jquery 来源: https://codeday.me/bug/20191101/1983050.html