下面搜集了五種方法,主要還是前兩個提供了解決方案,第三種需要修改jQuery源碼:
修複jquery.validate插件中name屬性相同(如name=’a[]‘)時驗證的bug
使用jquery.validate插件http://jqueryvalidation.org/,當節點的name相同時候,腳本特意忽略剩餘節點,導緻所有相關節點的errMsg都顯示在第一個相關節點上。這個bug在動态生成表單時候影響比較大。
通過查詢資料,找到一個解決方案:
http://stackoverflow.com/questions/931687/using-jquery-validate-plugin-to-validate-multiple-form-fields-with-identical-nam
具體内容為
$(function () {
if ($.validator) {
//fix: when several input elements shares the same name, but has different id-ies....
$.validator.prototype.elements = function () {
var validator = this,
rulesCache = {};
// select all valid inputs inside the form (no submit or reset buttons)
// workaround $Query([]).add until http://dev.jquery.com/ticket/2114 is solved
return $([]).add(this.currentForm.elements)
.filter(":input")
.not(":submit, :reset, :image, [disabled]")
.not(this.settings.ignore)
.filter(function () {
var elementIdentification = this.id || this.name;
!elementIdentification && validator.settings.debug && window.console && console.error("%o has no id nor name assigned", this);
// select only the first element for each name, and only those with rules specified
if (elementIdentification in rulesCache || !validator.objectLength($(this).rules()))
return false;
rulesCache[elementIdentification] = true;
return true;
});
};
}
});
在頁面上引入以上代碼,然後給相關節點加上id屬性,當name屬性相同時候會以id屬性來驗證
-------------------------------------------------------------------------------------------
用下面這種方式應該能解決:(http://stackoverflow.com/questions/2589670/using-jquery-validate-with-multiple-fields-of-the-same-name)
$(function(){
$("#myform").validate();
$("[name=field]").each(function(){
$(this).rules("add", {
required: true,
email: true,
messages: {
required: "Specify a valid email"
}
});
});
});
----------------------------------------------------------------------------------
jquery.validate.js 相同name的多個元素隻能驗證第一個元素的解決辦法
動态生成的相同name的元素驗證隻會取第一個.
很惱火的問題.隻有将jquery.validate.js中的對相同name的元素判斷注釋掉.
但是不知道會不會引起其他地方的BUG
希望以後jquery.validate.js能做針對元素ID進行驗證而不僅僅針對元素name驗證.
方法:
将484行的代碼注釋掉即可
// select only the first element for each name, and only those with rules specified if ( this.name in rulesCache || !validator.objectLength($(this).rules()) ) { return false; }
// select only the first element for each name, and only those with rules specified
-----------------------------------------------------------------------------------------------------------------------------------------
<html>
<head>
<link href="style.css" target="_blank" rel="external nofollow" rel="stylesheet">
<script src="assets/js/jquery-1.7.1.min.js"></script>
<script src="assets/js/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#contact-form').validate();
$(":text").each( function(){
$(this).rules( "add", {
required:true,
minlength: 2
})
});
});
</script>
</head>
<body>
<form action="" id="contact-form" class="form-horizontal">
<p>
<input type="text" name="test_a" id="a"><br>
<input type="text" name="test_a" id="b"><br>
<input type="text" name="test_a" id="c"><br>
<button type="submit" class="btn btn-primary btn-large">Submit</button>
<button type="reset" class="btn">Cancel</button>
</form>
</body>
</html>
這個表單的input 是随機生成的,是以name都是相同的,我現在要用jquery.validate.js來驗證輸入,現在隻校驗了第一id=‘a' 的,怎麼讓我驗證所有的?
你這麼寫其實是添加驗證成功的了,驗證會被執行,隻是submit的時候不是你想要的效果。
你可以試試,輸入第一個框後,在第二個框裡點一下不輸入再點到第三個框。
可以看到驗證的邏輯被執行了。
分析一下原因:
jquery.validate 這個插件在生成rules的時候是按name來生成的,也就是說,你的表單其實隻添加了一條驗證rule:就是對name=test_a的字段做非空和最小長度驗證。
當輸入框失去焦點時會觸發這條規則,因為每個input的name都是test_a,可以命中rules中的規則
當submit的時候,同樣會調用{'test_a': { required:true, minlength: 2}}這條規則, 隻不過這條規則會被通過,因為已經有一個test_a字段達到了規則的要求。
追問
那怎麼實作submit的時候全部校驗呢?
回答
修改input的name, 動态生成不同的name
追問
我使用class的方式還是隻檢驗一個啊?求解
回答
嗯,我也試了,是不行。是以建議修改name, 或者不用jq的插件
---------------------------------------------------------------------------------------------------------------------------------------------
function validate()
{
var result=true;
$("input[name='你定義的name']").each(
function(){
if($(this).val()=="")
{
alert("請輸入");
$(this).focus();
result=false;
return;
}
}
);
return result;
}
參考文章:http://blog.csdn.net/zoutongyuan/article/details/28094565
關注公衆号,分享幹貨,讨論技術,你的支援是我最大的動力!!!
