2011-05-27 41 views
1

我正在使用jquery驗證插件來驗證ajax submited窗體。如果有一些錯誤的服務器端,ajax調用返回表單的html。jquery驗證插件裏面的ajax加載內容

問題是,如果這發生在ajax doens't工作了。我需要像jquery validation插件一樣的「live」jquery函數。

這裏是我的代碼:

$('#myform').validate({ 
      submitHandler: function(form) { 
       formData = $(form).serialize(); 


       $.ajax({ 
        type: "POST", 
        url: form.action, 
        data: formData , 
        dataType: 'json', 
        success: function(response){ 
         if(response.status == "success") 
          window.location.reload(true); 
         else 
          $('#myformdiv').html(response.html); 

        }, 
        error: function(response){ 

        } 
       }); 

       return false; 
      } 
     }); 

回答

-3

你應該考慮讀this question和我的答案來

+0

指向僅包含斷開鏈接的其他答案的鏈接。不是很有用。 – 2017-09-01 13:23:28

1

您需要重新運行驗證腳本由內HTML包括後。

我有同樣的問題。表單驗證正在工作,當我在頁面加載時從服務器加載它,但在ajax驗證失敗後。所以我在將表單嵌入到html並添加它之後添加了這個代碼。

$('#myform').validate({ 
     submitHandler: function(form) { 
      formData = $(form).serialize(); 


      $.ajax({ 
      type: "POST", 
      url: form.action, 
      data: formData , 
      dataType: "html", 
      cache: false, 
      success: function (data) { 
       $('#myformdiv').html(data); 
       $.getScript("/Scripts/jquery.validate.unobtrusive.js"); 
      }, 
      error: function (xhr, status, error) { 
       alert(xhr.responseText); 
      } 
      }); 

      return false; 
     } 
    });