2013-07-08 46 views
0

我正在使用JQuery和AJAX提交和處理來自。當用戶點擊類似按鈕時,通過AJAX發送給我的like.php文件。 like.php文件處理請求,併爲不同按鈕創建html。我的成功函數使用類似按鈕的html,並用新的不同按鈕替換類似按鈕。除了當我用AJAX和JQuery替換類似按鈕時,不同按鈕的結構消失了。我可能做錯了什麼?使用ajax時造型麻煩

jQuery和AJAX:

$(document).ready(function() { 
    $(".like-form").submit(function() { 

     var dataString = $(this).serialize(); 
     var $this = $(this); 
     $.ajax({ 
      type: "POST", 
      url: "like.php", 
      data: dataString, 
      success: function(html) { 
       $this.replaceWith(html); 
      } 
     }); 

     return false; 

    }); 
}); 

原始形態:

<form class="like-form" method="post" action=""> 
    <div class="post-button"> 
     <button type="submit" type="submit" name="like" 
     class="like-button"/><p> Like </p></button> 
    </div> 
</form> 

Like.php:

// Code to process the form 

//This replaces the original form on submit 
<form class="like-form" method="post" action=""> 
    <div class="post-button"> 
     <button type="submit" type="submit" name="unlike" 
     class="like-button"/><p> Unlike </p></button> 
    </div> 
</form> 
+0

嘗試不同按鈕添加後提高標記來改變內容 - '$(文件).ajaxComplete(函數(){ 嘗試{ FB.XFBML.parse(); } catch(ex){} });' – Ross

+0

你的意思是「...不同按鈕的結構已經消失了......」 - 你的意思是沒有放入表單中? –

+0

它失去了它的造型 – user2320500

回答

0

也許這不是既喜歡又不像正確HTML:

<form class="like-form" method="post" action=""> 
    <div class="post-button"> 
     <button type="submit" name="Unlike" class="like-button"/> 
<!-- two "type=" 
      closed with /> and an additional </button> at the end 
--> 
    </div> 
</form> 

嘗試用$this.html(html);