首页 > 解决方案 > ajax删除不适用于onclick事件

问题描述

我需要一些帮助,请我一直在搜索并试图知道为什么当我单击该按钮时什么都没有发生

html:

<td></td>
<td></td>
<td></td>
<td><button id="<?php echo($row['ID']); ?>" onClick="delord()" class="del" style="font-size: 12">delete</button></td>

jQuery:

function delord(){
    var x = event.target.id;
    $.ajax({
        url: 'delorder.php?id=' + x,
        success: function(){
            alert('deleted');
        }
    });
}

我尝试alert(x);在我的 jquery 代码中输入并返回值然后我尝试转到“delorder.php?id=335”并且该行已成功删除

只是当我用 ajax 尝试它时它不起作用

标签: phpjqueryhtmlajax

解决方案


做这些

     <table>
        <thead>
            <tr>
                <td>Name</td>
                <td></td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Stephen</td>
                <td><button onclick="deleteRow('delete',<?php echo($row['ID']); ?>)">Delete</button></td>
            </tr>
        </tbody>
    </table>

jQuery:

function deleteRow(action,id){
  $.ajax({
    url: 'delorder.php?id=' + id,
    success: function(){
        alert('deleted');
    }
  });
 }

推荐阅读