2017-09-06 111 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 >