使用 jquery 验证器验证多个具有相同名称的文本框仅验证第一个输入 [英] validating multiple textbox having same name with jquery validator validates only first input
问题描述
我正在尝试使用 jquery 验证器验证具有相同名称的两个输入..
但是当我运行这段代码时,它只验证第一个输入,第二个输入被简单地忽略
我也尝试过使用 <input type='text' name='inp_text[]'/><br>
但仍然无法正常工作.
我应该改变什么...?
提前致谢
我正在尝试使用 jquery 验证具有相同名称的两个输入验证器..."
<input type='text' name='inp_text'/>
<块引用>
"但是当我运行这段代码时,它只验证第一个输入和第二个输入被简单地忽略"
您不能有两个 type="text"
的 input
字段具有相同的 name
否则此插件将无法正常工作.name
属性必须是唯一的.(name
唯一的一个例外是,复选框或单选输入的分组"将共享相同的 name
,因为相应的提交是单个点数据.但是,name
对于每组复选框和单选元素仍然必须是唯一的.)
我应该改变什么......?"
使每个 name
属性唯一.
<input type='text' name='inp_text[2]'/>
然后使用开始于"选择器,^=
...
$("[name^=inp_text]").each(function () {$(this).rules("添加", {要求:真实,检查值:真});});
工作演示:http://jsfiddle.net/PgLh3/
注意:当使用 rules('add')
方法时,您还可以通过 id
定位元素,但是对于这种情况,没有已解决,因为插件仍然需要每个 input
元素上的 唯一 name
.
I am trying to validate the two inputs having same name with jquery validator ..
<script src='
http://code.jquery.com/jquery-latest.min.js '></script>
<script src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js'></script>
<script>
$(document).ready(function(){
$('#frm').validate();
$("[name=inp_text]").each(function(){
$(this).rules("add", {
required: true,
checkValue: true
});
});
$.validator.addMethod("checkValue", function(value, element) {
var response = ((value > 0)&&(value <= 100))||((value == 'test1')||(value =='test2'));
return response;
}, "invalid value");
})
</script>
<form id='frm' method='post'>
<input type='text' name='inp_text'/><br>
<input type='text' name='inp_text'/><br>
<input type='submit' name=''/>
</frm>
but when I run this code it only validates first inputs and second inputs gets simply ignored
I have also tried using <input type='text' name='inp_text[]'/><br>
but still not working.
what should I change...?
Thanks in advance
"I am trying to validate the two inputs having same name with jquery validator ..."
<input type='text' name='inp_text'/>
<input type='text' name='inp_text'/>
"but when I run this code it only validates first inputs and second inputs gets simply ignored"
You cannot have two input
fields of type="text"
with the same name
or this plugin will not work properly. The name
attribute must be unique. (One exception to the name
being unique, is that "groupings" of checkbox or radio inputs will share the same name
as the corresponding submission is a single point of data. However, the name
must still be unique to each grouping of checkbox and radio elements.)
"what should I change...?"
Make each name
attribute unique.
<input type='text' name='inp_text[1]'/>
<input type='text' name='inp_text[2]'/>
Then use the "starts with" selector, ^=
...
$("[name^=inp_text]").each(function () {
$(this).rules("add", {
required: true,
checkValue: true
});
});
Working DEMO: http://jsfiddle.net/PgLh3/
NOTES: You can also target elements by id
when using the rules('add')
method, however for this case, nothing is solved because the plugin still requires a unique name
on each input
element.
这篇关于使用 jquery 验证器验证多个具有相同名称的文本框仅验证第一个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!