2012-09-12 119 views
0

我使用jQuery 1.7.2版本,我提交使用Ajax這樣的。點擊不是Ajax請求後,工作

$("input#addPostSubmit").click(function(){ 
    $.ajax({ 
      //this is the php file that processes the data and send mail 
      url: BASE_URL+"post/ajaxcreate",  
      //POST method is used 
      type: "POST", 
      //pass the data   
      data: $("#addImage").serialize(),  
      //Do not cache the page 
      cache: false, 
      //success 
      success: function (html) { 
         var obj = jQuery.parseJSON(html);   
        if(obj.Status == 'Success'){ 
         $("#error").removeClass('hide'); 
         $("#error").addClass('success'); 
         $("#error").html(obj.Message); 
         $.fn.colorbox.resize(); 
         setTimeout(function(){$.fn.colorbox.close()},5000); 
         window.parent.location.reload();  
        } 
        if(obj.Status == 'Fail'){ 
         $('.add_post_submit').html('<input type="button" id="addPostSubmit" value="Create" />'); 
         $("input#addPostSubmit").removeAttr('disabled'); 
         $("#error").removeClass('hide'); 
         $("#error").addClass('error'); 
         error = obj.Message.split(';'); 
         html = '<ul>'; 
         for(a=0;a<error.length;a++){ 
          html += '<li>'+error[a]+'</li>'; 
         } 
         html += '</ul>';  
         $("#error").html(html); 
         $('html').css({overflow: 'hidden'}); 
         $.fn.colorbox.resize(); 
        } 
      }  
    }); 
}); 

我的存在,例如空不允許在表單域之一,當服務器驗證窗體和返回錯誤我顯示它時,當我再次點擊按鈕,它不工作...

它工作正常,但問題是當Ajax請求完成,服務器返回 任何形式的錯誤,然後提交按鈕沒有工作請幫助我,我卡住了....

+0

郵政**'ERROR MESSAGE' **,你從服務器得到 – thecodeparadox

+0

可能是你應該使用'返回false;'的'click'函數中的第一行,這樣的默認操作被取消出。 – SexyBeast

+0

給一個jsfiddle – Aravind

回答

2

我建議聆聽文檔上的提交事件,而不是點擊處理程序。

$(document).on('submit','form',function(e){ 
    e.preventDefault(); 
    $.ajax({ 
      //this is the php file that processes the data and send mail 
      url: BASE_URL+"post/ajaxcreate",  
      //POST method is used 
      type: "POST", 
      //pass the data   
      data: $("#addImage").serialize(),  
      //Do not cache the page 
      cache: false, 
      //success 
      success: function (html) { 
       // come code 
      }  
    }); 
}); 
+0

謝謝你爲我工作 –

+0

我有同樣的問題,但它沒有奏效幫助我 – Lijo