jQuery Validate驗證表單時多個name相同的元素只驗證第一個的解決方法
下面搜集了五種方法,主要還是前兩個提供了解決方案,第三種需要修改jQuery源碼:
修復(fù)jquery.validate
插件中name屬性相同(如name='a[]‘
)時驗證的bug
使用jQuery.validate
插件http://jqueryvalidation.org/,當(dāng)節(jié)點的name相同時候,腳本特意忽略剩余節(jié)點,導(dǎo)致所有相關(guān)節(jié)點的errMsg都顯示在第一個相關(guān)節(jié)點上。這個bug在動態(tài)生成表單時候影響比較大。
通過查詢資料,找到一個解決方案:
具體內(nèi)容為
$(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; }); }; } });
在頁面上引入以上代碼,然后給相關(guān)節(jié)點加上id屬性,當(dāng)name屬性相同時候會以id屬性來驗證
-------------------------------------------------------------------------------------------
用下面這種方式應(yīng)該能解決:(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的多個元素只能驗證第一個元素的解決辦法
動態(tài)生成的相同name的元素驗證只會取第一個.
很惱火的問題.只有將jquery.validate.js中的對相同name的元素判斷注釋掉.
但是不知道會不會引起其他地方的BUG
希望以后jquery.validate.js能做針對元素ID進(jìn)行驗證而不僅僅針對元素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 /*if ( this.name in rulesCache || !validator.objectLength($(this).rules()) ) { return false; } */
-----------------------------------------------------------------------------------------------------------------------------------------
<html> <head> <link href="style.css" 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都是相同的,我現(xiàn)在要用jquery.validate.js來驗證輸入,現(xiàn)在只校驗了第一id=‘a(chǎn)' 的,怎么讓我驗證所有的?
你這么寫其實是添加驗證成功的了,驗證會被執(zhí)行,只是submit的時候不是你想要的效果。
你可以試試,輸入第一個框后,在第二個框里點一下不輸入再點到第三個框。
可以看到驗證的邏輯被執(zhí)行了。
分析一下原因:
jquery.validate
這個插件在生成rules的時候是按name來生成的,也就是說,你的表單其實只添加了一條驗證rule:就是對name=test_a
的字段做非空和最小長度驗證。
當(dāng)輸入框失去焦點時會觸發(fā)這條規(guī)則,因為每個input的name都是test_a,可以命中rules中的規(guī)則
當(dāng)submit的時候,同樣會調(diào)用{'test_a': { required:true, minlength: 2}}
這條規(guī)則, 只不過這條規(guī)則會被通過,因為已經(jīng)有一個test_a字段達(dá)到了規(guī)則的要求。
追問
那怎么實現(xiàn)submit的時候全部校驗?zāi)兀?br />
回答
修改input的name, 動態(tài)生成不同的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; }
以上所述是小編給大家介紹的jQuery Validate驗證表單時多個name相同的元素只驗證第一個的問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
jQuery中$.ajax()和$.getJson()同步處理詳解
這篇文章主要介紹了jQuery中$.ajax()和$.getJson()同步處理詳解的相關(guān)資料,非常的細(xì)致全面,有需要的小伙伴可以參考下。2015-08-08修改jquery里的dialog對話框插件為框架頁(iframe) 的方法
為什么我不直接用modal form來做呢?所以我就做了個jquery下面dialog的插件,需要引用原來dialog的文件。2010-09-09基于jQuery實現(xiàn)咖啡訂單管理簡單應(yīng)用
這篇文章主要為大家詳細(xì)介紹了基于jQuery實現(xiàn)咖啡訂單管理的簡單應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02jQuery實現(xiàn)form表單reset按鈕重置清空表單功能
有時候可能需要實現(xiàn)這樣的效果:使用ajax提交表單,成功提交表單之后清空表單,這種功能大家可能都希望實現(xiàn)吧,接下來為您詳細(xì)介紹,需要了解的朋友參考下2012-12-12