Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery on()并删除不工作的父项_Jquery - Fatal编程技术网

jQuery on()并删除不工作的父项

jQuery on()并删除不工作的父项,jquery,Jquery,我们有以下代码 HTML $(文档).ready(函数(){ $('.details')。在('单击','.remove',函数()上{ $.ajax({ url:“删除”, 键入:“GET”, 成功:功能(事件){ event.preventDefault(); $(this).tr.remove(); } }); } }); ... 点击我! 点击我! 点击我! 不幸的是,这不起作用,如何删除按钮的位置?(当然函数可以工作,例如:alert()) 更新:现在检查…代码中存在不同的问题 1

我们有以下代码

HTML


$(文档).ready(函数(){
$('.details')。在('单击','.remove',函数()上{
$.ajax({
url:“删除”,
键入:“GET”,
成功:功能(事件){
event.preventDefault();
$(this).tr.remove();
}
});
}
});
...
点击我!
点击我!
点击我!
不幸的是,这不起作用,如何删除按钮的位置?(当然函数可以工作,例如:alert())


更新:现在检查…

代码中存在不同的问题

1,您的html无效。您需要将按钮包装在
tr
td

2、没有类为
.details
的元素。如果有此类元素,则它应该是表的父元素。然后,只有事件委派才能工作

3、您可以使用
.closest()
代替callong
parent()
两次

您的html应该如下所示

<table>
    <thead>
        ...
    </thead>
    <tbody>
        <tr>
            <td>
                <button class="remove" value="somevalue1">clickme!</button>
            </td>
        </tr>
        <tr>
            <tr>
                <td>
                    <button class="remove" value="somevalue2">clickme!</button>
                </td>
            </tr>
            <tr>
                <td>
                    <button class="remove" value="somevalue3">clickme!</button>
                </td>
            </tr>
    </tbody>
</table>
在上面的示例中,我将click事件委托给
文档
。您可以用dom ready上的任何其他父元素替换它

在您的editerd代码中,
$(this)
引用的是ajax,而不是单击的元素。因此您需要暂时保存它,以便在成功处理程序中引用它

$('.details').on('click', '.remove', function(event) {
   event.preventDefault();
    var obj = $(this)
    $.ajax({
        url: 'storageorder-inventory-remove',
        type: 'GET',
        success: function() {

            $(obj).closest("tr").remove();
        }
    });

});

你的html无效。t正文中应该有tr和td。你的html中没有tr。我也没有在你的html中看到
.details
元素。天哪,讨厌我。我只是复制/粘贴(html中的一些数据是由函数生成的,比如按钮).无论如何,我更新了我的代码,现在检查一下。对不起,对不起,很好,谢谢。给我一个减号,我的愚蠢。谢谢
$(document).on("click", ".remove", function(e) {
    e.preventDefault();
    $(this).closest("tr").remove();

});
$('.details').on('click', '.remove', function(event) {
   event.preventDefault();
    var obj = $(this)
    $.ajax({
        url: 'storageorder-inventory-remove',
        type: 'GET',
        success: function() {

            $(obj).closest("tr").remove();
        }
    });

});