简体   繁体   English

删除单击元素的父级

[英]Delete parent of clicked element

I'm trying to "hide" an element when an ajax to a file returns true. 当文件的Ajax返回true时,我试图“隐藏”一个元素。 When I click on the id of #delete , an ajax request is sent to a file that handles data which will return passed or failed. 当我单击#delete的ID时,Ajax请求将发送到处理数据的文件,该数据将返回通过或失败。 If the file returns 'passed', I then want to hide the parent to show it was 'deleted'. 如果文件返回“通过”,则我想隐藏父文件以显示它已被“删除”。

It's a list of items that are displayed dynamically. 这是动态显示的项目列表。 Each item has an 'x' that when clicked, the user is prompted to hit confirm or cancel to remove that item from the list. 每个项目都有一个“ x”,当单击该项目时,系统会提示用户单击“确认”或“取消”以将其从列表中删除。 When the user hits confirmed, an ajax request is called to a file that will send the string 'passed' or 'failed'. 当用户点击确认时,将向文件发送一个ajax请求,该文件将发送字符串“ passed”或“ failed”。

This is what happens on the javascript side: 这是在JavaScript端发生的情况:

$('#delete').on('click', function() {

    var $that = $('#delete');

    bootbox.confirm("Are you sure you want to delete this event?", function(r) {

        // r is the result, true or false

        if(r) {

            $.post('panel', {"id": $that.data('id')}, function(data) {

                if(data.status === 'passed') {

                    $that.parent().fadeOut();

                }

            });

        }

    })

});

All this works, but only for the first list item. 所有这些都有效,但仅适用于第一个列表项。 If I click #delete on the second or third item, nothing happens. 如果我在第二或第三项上单击#delete ,则什么也不会发生。 If I click on the first item and it successfully removes the parent, I cannot do the same for the other items. 如果我单击第一个项目并成功删除了父项,则我无法对其他项目执行相同的操作。


<li>
    <h4>Staff Meeting</h4>

    <p>

        This is a staff meeting 

    </p>

    <cite>
        <i class="fa fa-clock-o"></i> 
        1 day from now
    </cite>

    <span id="delete" data-id="1"><i class="fa fa-times"></i></span>
    <span id="edit" data-id="1"><i class="fa fa-edit"></i></span>

</li>


<li>
    <h4>Staff Meeting</h4>

    <p>

        This is another staff meeting   

    </p>

    <cite>
        <i class="fa fa-clock-o"></i> 
        16 hours ago
    </cite>

    <span id="delete" data-id="2"><i class="fa fa-times"></i></span>
    <span id="edit" data-id="2"><i class="fa fa-edit"></i></span>

</li>

The data sent to the file is data-id which represents the id of the item. 发送到文件的data-iddata-id ,它表示项目的ID。 On the PHP side, that's where the item with the id specified is deleted from the database. 在PHP方面,从数据库中删除具有指定ID的项目。 If it is deleted successfully, I return a json response of {"status": "passed"} and hide the list item on the client side showing it was successfully deleted. 如果删除成功,我将返回json响应{"status": "passed"}并在客户端隐藏列表项,表明已成功删除。

EDIT 编辑

Now that the id has been changed to class , on success all items are deleted. 现在,将id更改为class ,成功后,所有项目都将被删除。 I only need the current clicked element's parent deleted. 我只需要删除当前单击元素的父级。

id s must be unique. id必须是唯一的。 The handler is (correctly) only being applied to the first id="delete" element it finds. 该处理程序仅正确地应用于它找到的第一个id="delete"元素。 If you want to apply the same handler to multiple delete buttons, give them a shared class and apply the handler to that: 如果要将同一处理程序应用于多个删除按钮,请为它们提供一个共享类,然后将处理程序应用于该按钮:

<span class="delete" data-id="1"><i class="fa fa-times"></i></span>
$('.delete').on('click', function() {

    var $that = $(this);  

    bootbox.confirm("Are you sure you want to delete this event?", function(r) {

        // r is the result, true or false

        if(r) {

            $.post('panel', {"id": $that.data('id')}, function(data) {

                if(data.status === 'passed') {

                    $that.parent().fadeOut();

                }

            });

        }

    })

});

Ids are meant to be unique in a document. ID在文档中应唯一。 Try having delete be a class instead. 尝试让delete成为类。

To refer to just the item that was clicked use $(this) 要仅引用单击的项目,请使用$(this)

$().on('click', '.delete' function() {

  var $that = $(this);
  ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM