国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery Validate驗證表單時多個name相同的元素只驗證第一個的解決方法

2019-11-19 18:17:10
字體:
來源:轉載
供稿:網友

下面搜集了五種方法,主要還是前兩個提供了解決方案,第三種需要修改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 /*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都是相同的,我現在要用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;}

以上所述是小編給大家介紹的jQuery Validate驗證表單時多個name相同的元素只驗證第一個的問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新和县| 板桥市| 花莲市| 廊坊市| 昌乐县| 木兰县| 缙云县| 垦利县| 桂林市| 山西省| 江陵县| 荃湾区| 金寨县| 浦县| 田东县| 江安县| 襄垣县| 武安市| 河曲县| 永平县| 都江堰市| 炉霍县| 安义县| 准格尔旗| 攀枝花市| 阿拉善右旗| 望城县| 威信县| 定州市| 信宜市| 红原县| 蓝山县| 平果县| 武平县| 佛冈县| 宝坻区| 双牌县| 巩留县| 界首市| 泸水县| 永善县|