Jquery validate验证表单时多个name相同的元素只验证第一个的问题
修復(fù)jquery.validate插件中name屬性相同(如name=’a[]‘)時(shí)驗(yàn)證的bug
使用jquery.validate插件http://jqueryvalidation.org/,當(dāng)節(jié)點(diǎn)的name相同時(shí)候,腳本特意忽略剩余節(jié)點(diǎn),導(dǎo)致所有相關(guān)節(jié)點(diǎn)的errMsg都顯示在第一個(gè)相關(guān)節(jié)點(diǎn)上。這個(gè)bug在動(dòng)態(tài)生成表單時(shí)候影響比較大。
通過(guò)查詢資料,找到一個(gè)解決方案:
http://stackoverflow.com/questions/931687/using-jquery-validate-plugin-to-validate-multiple-form-fields-with-identical-nam
具體內(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 solvedreturn $([]).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 specifiedif (elementIdentification in rulesCache || !validator.objectLength($(this).rules()))return false;rulesCache[elementIdentification] = true;return true;});}; } });在頁(yè)面上引入以上代碼,然后給相關(guān)節(jié)點(diǎn)加上id屬性,當(dāng)name屬性相同時(shí)候會(huì)以id屬性來(lái)驗(yàn)證
-------------------------------------------------------------------------------------------用下面這種方式應(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的多個(gè)元素只能驗(yàn)證第一個(gè)元素的解決辦法動(dòng)態(tài)生成的相同name的元素驗(yàn)證只會(huì)取第一個(gè).
很惱火的問(wèn)題.只有將jquery.validate.js中的對(duì)相同name的元素判斷注釋掉.
但是不知道會(huì)不會(huì)引起其他地方的BUG
希望以后jquery.validate.js能做針對(duì)元素ID進(jìn)行驗(yàn)證而不僅僅針對(duì)元素name驗(yàn)證.
方法:
將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> 這個(gè)表單的input 是隨機(jī)生成的,所以name都是相同的,我現(xiàn)在要用jquery.validate.js來(lái)驗(yàn)證輸入,現(xiàn)在只校驗(yàn)了第一id=‘a(chǎn)' 的,怎么讓我驗(yàn)證所有的?你這么寫其實(shí)是添加驗(yàn)證成功的了,驗(yàn)證會(huì)被執(zhí)行,只是submit的時(shí)候不是你想要的效果。你可以試試,輸入第一個(gè)框后,在第二個(gè)框里點(diǎn)一下不輸入再點(diǎn)到第三個(gè)框。 可以看到驗(yàn)證的邏輯被執(zhí)行了。分析一下原因:jquery.validate 這個(gè)插件在生成rules的時(shí)候是按name來(lái)生成的,也就是說(shuō),你的表單其實(shí)只添加了一條驗(yàn)證rule:就是對(duì)name=test_a的字段做非空和最小長(zhǎng)度驗(yàn)證。當(dāng)輸入框失去焦點(diǎn)時(shí)會(huì)觸發(fā)這條規(guī)則,因?yàn)槊總€(gè)input的name都是test_a,可以命中rules中的規(guī)則當(dāng)submit的時(shí)候,同樣會(huì)調(diào)用{'test_a': { required:true, minlength: 2}}這條規(guī)則, 只不過(guò)這條規(guī)則會(huì)被通過(guò),因?yàn)橐呀?jīng)有一個(gè)test_a字段達(dá)到了規(guī)則的要求。追問(wèn)那怎么實(shí)現(xiàn)submit的時(shí)候全部校驗(yàn)?zāi)?#xff1f;回答修改input的name, 動(dòng)態(tài)生成不同的name追問(wèn)我使用class的方式還是只檢驗(yàn)一個(gè)啊?求解回答嗯,我也試了,是不行。所以建議修改name, 或者不用jq的插件---------------------------------------------------------------------------------------------------------------------------------------------function validate() {var result=true;$("input[name='你定義的name']").each(function(){if($(this).val()==""){alert("請(qǐng)輸入");$(this).focus();result=false;return;} } );return result; }參考文章:http://blog.csdn.net/zoutongyuan/article/details/28094565關(guān)注公眾號(hào),分享干貨,討論技術(shù)
轉(zhuǎn)載于:https://www.cnblogs.com/molashaonian/p/9097589.html
總結(jié)
以上是生活随笔為你收集整理的Jquery validate验证表单时多个name相同的元素只验证第一个的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: NEFU 560 半数集
- 下一篇: std::priority_queue