2014-05-11 34 views
0

如何使用jQuery驗證插件獲取對提交按鈕的引用?如何使用jQuery驗證插件獲取提交按鈕的引用?

function defSubmHendler(){ 
    $myform.validate({ 
     rules: rules, 
     messages: messages, 
     submitHandler: function() { 
      event.preventDefault(); 
      if (~this form id == A){ 
       condition1; 
      } 
      else if (~this form id == B){ 
       condition2; 
      } else {} 
      ... 
      $.ajax({ 
       ... 
      }); 
     } 
    }); 
} 

在這種情況下這個 == $ .validator。我需要參考submitHandler中的提交按鈕/表單。

+0

你是怎麼調用'defSubmitHandler'的? – Ejaz

+0

動態創建表單,然後執行defSubmHendler('destination_form') – kvadrat

回答

1

考慮給一個id到您的提交按鈕,並引用它,像這樣:

submitHandler: function() { 
    //Your submit button referenced by its ID 
    $("#mySubmitButton").doStuff(); 
} 

或者,您submitHandler功能使用form元素,並找到包含在該表單的提交按鈕:

submitHandler: function(form) { 
    //Via the form argument passed to the submitHandler 
    var btnSubmit = form.find(":submit"); 
} 
+0

表單是動態創建的。處理程序不知道此按鈕屬於哪種形式。我的例子可能不正確。 $ myform是可變的 – kvadrat

相關問題