欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery Validate驗(yàn)證表單時(shí)多個(gè)name相同的元素只驗(yàn)證第一個(gè)的解決方法

 更新時(shí)間:2016年12月24日 16:59:45   作者:molashaonian  
這篇文章主要介紹了jQuery Validate驗(yàn)證表單時(shí)多個(gè)name相同的元素只驗(yàn)證第一個(gè)的問題及解決方法,需要的朋友可以參考下

下面搜集了五種方法,主要還是前兩個(gè)提供了解決方案,第三種需要修改jQuery源碼:

修復(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í)候影響比較大。

通過查詢資料,找到一個(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 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;
 });
 };
}
});

在頁(yè)面上引入以上代碼,然后給相關(guān)節(jié)點(diǎn)加上id屬性,當(dāng)name屬性相同時(shí)候會(huì)以id屬性來驗(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è).

很惱火的問題.只有將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來驗(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來生成的,也就是說,你的表單其實(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ī)則會(huì)被通過,因?yàn)橐呀?jīng)有一個(gè)test_a字段達(dá)到了規(guī)則的要求。

追問

那怎么實(shí)現(xiàn)submit的時(shí)候全部校驗(yàn)?zāi)兀?br />

回答

修改input的name, 動(dòng)態(tài)生成不同的name

追問

我使用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;
}

以上所述是小編給大家介紹的jQuery Validate驗(yàn)證表單時(shí)多個(gè)name相同的元素只驗(yàn)證第一個(gè)的問題,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論