2016-11-08 20 views
-2

我有一個表格。我想檢查表單ID。 因爲我不知道表單ID。如何用Jquery檢查表單ID?

我嘗試這樣的:

jQuery.validator.addMethod("checkDuplicate",function(value) { 
     var user_id = $(this).closest("form[id]").attr('id'); 
     console.log(user_id); 
     var ka = $('#ID').val(); 
     if ($("#contactpersonen_canseestock_2").is(':checked') || $("#contactpersonen_canseestock_3").is(':checked') || $("#contactpersonen_canseestock_4").is(':checked') 
|| $("#contactpersonen_canseestock_5").is(':checked')) && $('#idform :checkbox:checked').length == 1) { 
     console.log("checkbox checked; dont check duplicate email"); 
     return true; 
     } 
     else{ 
     console.log("checkbox not checked; check duplicate email"); 
     if ($("#contactpersonen_canseestock_2").is(":checked")) 
     { 
      console.log('hoi'); 
} 
     console.log(ka); 
     return false; 
     } 
    }, "Email adres bestaat al"); 

    jQuery.validator.classRuleSettings.checkTotal = { checkTotal: true }; 

因爲#idform不是形式的ID。

但我每次都得到未定義。當我這樣做:

var user_id = $(this).closest("form[id]").attr('id'); 

這是形式:

http://webshop.verploegen.nl/klant-worden

,但我不能看到表格ID是什麼

這是selecter checkDuplicate:

<div class="contact-label span2"> 
      <label for="contactpersonen-email">Email adres</label> 
      <div class="contact-input-field"> 
      <input type="text" class="input-text span2 contactpersonen-email1 required checkDuplicate" id="contactpersonen_email1" name="contactpersonen_email1"></input> 
      </div> 
     </div> 
+1

貴''

有'id'?看起來很奇怪,爲表單調用變量'user_id'。 ''?? –

+1

請改用'var user_id = $(this).closest(「form」)。attr('id');' –

+0

@RahulPatel你能解釋一下嗎?如果'.attr(「id」)'會起作用,那麼'$(「form [id]」)''也可以。 –

回答

3

我看着jQuery.validator.addMethod()規範,我看到第È功能有三個參數:

  • 值=驗證的元件的電流值
  • 元素=元件進行驗證
  • PARAMS = ...

如果有一個元件參數意味着this不是針對元素,而是針對其他內容。

所以,如果我們做

$(element).closest("form[id]").attr('id') 

我認爲它會工作

+0

謝謝。我試圖說:var user_id = $(this).closest(「form」)。attr('id'); \t \t console.log(user_id);但仍未定義。我編輯文章 – SavantCode

+0

我更新了帖子。有完整的表格 – SavantCode

+0

不知道你爲什麼認爲'form [id]'是一個無效的選擇器,沒關係:https://jsfiddle.net/s1rhcgfm/ –

相關問題