2013-07-25 37 views
2

我使用asp .net mvc 4,我有一個html表單使用ajax驗證。asp.net驗證與jQuery的成功回調

我用這個javascript代碼來知道表單是否無效。此代碼有效。

$('#form').bind('invalid-form.validate', function() { 
      alert("error"); 
      return false; 
     }); 

但我無法找到成功的JavaScript回調?

我想:

$('#form').bind('valid-form.validate', function() { 
      alert("success"); 

     }); 

$('#form').validate({ 
       invalidHandler: 
       function (form, validator) { 
        alert('error'); 
       }, 
       rules: function (form, validator) { 
        alert('success '); 
       } 
      }); 

但我從來沒有獲得成功的警報。謝謝你的幫助。

+0

你在做的Ajax表單發佈,還是定期?你希望彈出窗口在服務器返回之前還是之後出現? – McGarnagle

+0

其實我不想使用alert函數,現在只是爲了調試,我想通過做這樣的$(「myid」)。hide()來隱藏div。但我需要知道它是否成功 – user2037696

回答

1

使用ajax.beginform

@using (Ajax.BeginForm("Delete",new{id = Model.id}, new AjaxOptions 
    { 
    OnSuccess = "OnSuccess", 
    OnFailure = "OnFailure", 
    HttpMethod="Post" 
})) 

然後更改的onSuccess/onFailure處到Java腳本函數的名稱

0

綁定到form.submit,而不是這樣的 -

$('#form').on('submit', function (e) { 
    e.preventDefault(); 
     if ($(this).valid()) { // This checks if the form is valid 
      alert("success"); // Or instead of success you can hide the form here 
      $.ajax({ 
       url: this.action, 
       type: this.method, 
       data: $(this).serialize(), 
       success: function (result) { 
        // Goes in here after your MVC controller action is done 
       }, 
       error: function (result) { 
        // Goes in here if your mvc controller action returns an error     
       } 
      }); 
     }; 
});