2017-09-06 151 views
0

我有動態按鈕,從數據庫生成。 例如:點擊後通過id刪除元素

button 1, id=button1 
button 2, id= button2 
button 3, id= button3 
..etc.... 

如果我點擊按鈕1,我的功能會送東西給mysql數據庫,所以我叫AJAX。 但我想要的是:如何在進程發送到數據庫完成後刪除button1? 與其他按鈕同樣的事情(送東西到數據庫,並刪除按鈕)

在我的按鈕,我把的onClick

這裏

是我的代碼:

<script> 
    function process(idbutton){ 
    $.ajax({ 
        type: "POST", 
        url: "http://urlofmysite.com/process.php?", 
       data: "pidd=" + idbutton, 
        cache: false, 
        success: function(html){ 
        alert(html); //RESPONSE FROM SERVER  
        } 
       }); 
    //REMOVING BUTTON 
    document.getElementById(+idbutton+).outerHTML=""; 
    } 
</script> 

,但它使用犯規的jQuery Mobile的工作phonegap

謝謝

+0

對不起,現在工作。我改變document.getElementById(+ idbutton +)。outerHTML =「」;到document.getElementById(idbutton).outerHTML =「」; – Def

+0

只是把它放在你的''成功'回調'$('#'+ idbutton).remove()'中。這是更好的,因爲你已經使用jQuery,也只有在成功調用你的url時纔會刪除按鈕 –

回答

0

你應該使用你擁有的。 首先,ajax調用的回調爲success。你應該刪除你的按鈕。如果ajax調用不起作用(服務器無法訪問),您的按鈕將保持不變。 其次,你在你的項目中使用jQuery。不要混用jQuery和本地Dom操作函數。使用簡單易讀的jQuery功能$('#'+idbutton).remove()刪除您的按鈕。

<script> 
function process(idbutton) { 
    $.ajax({ 
     type: "POST", 
     url: "http://urlofmysite.com/process.php?", 
     data: "pidd=" + idbutton, 
     cache: false, 
     success: function (html) { 
      alert(html); //RESPONSE FROM SERVER 
      //REMOVING BUTTON 
      $('#' + idbutton).remove(); 
     } 
    }); 

} 
</script >