Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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:在选择/操作父元素时遇到问题_Jquery_Select_Parent - Fatal编程技术网

jQuery:在选择/操作父元素时遇到问题

jQuery:在选择/操作父元素时遇到问题,jquery,select,parent,Jquery,Select,Parent,我第一次使用jQuery和AJAX,在尝试从数据库中删除条目时似乎遇到了一个小障碍。或者,更清楚地说,该项已成功地从数据库中删除,但我似乎无法选择删除后要隐藏的正确元素 下面是一些示例代码的样子(在本例中,我只是为了测试目的尝试更改背景颜色:我将在我的生产环境中使用淡出(fadeOut)或等效代码——下面将有更多解释): 为了澄清它在前端的外观,我的ULs被设置为display:table row,我的LIs被设置为display:table cell,我有一个包含它们的DIV,并被设置为dis

我第一次使用jQuery和AJAX,在尝试从数据库中删除条目时似乎遇到了一个小障碍。或者,更清楚地说,该项已成功地从数据库中删除,但我似乎无法选择删除后要隐藏的正确元素

下面是一些示例代码的样子(在本例中,我只是为了测试目的尝试更改背景颜色:我将在我的生产环境中使用淡出(fadeOut)或等效代码——下面将有更多解释):

为了澄清它在前端的外观,我的ULs被设置为display:table row,我的LIs被设置为display:table cell,我有一个包含它们的DIV,并被设置为display:table。基本上,我有一个UL每记录和一个李每列在每个记录


谢谢

AJAX回调中有不同的上下文。所以,$(这个)指向另一个对象。我建议您在以下示例中使用闭包变量“item”:

//When link with class .deleter is clicked, get the parent element's id (which is also that element's id in the database)
$('.deleter').click(function() {
    var item = $(this).closest('ul');
    var recordToDelete = item.attr('id');

//Send the id to the PHP script, which returns 1 if successful and 0 if not
    $.post("databasefuncs.php?func=delete", { postedMessage : recordToDelete },
        function(result){
            if(result == 1) {
                //Change the background color of the parent UL to red if successful
                item.css('background-color', 'red');
            } else {
                //Change the background color of the parent UL to blue if successful
                item.css('background-color', 'blue');
            }

        });
});

啊。这就解释了。我最终还是以同样的方式工作,但我认为你的方式可能更符合惯例。想提供一些见解吗?这边行吗?如果(result==1){$(“#”+recordToDelete).css('background-color','red').fadeOut('fast');}或者{$(“#”+recordToDelete.fadeOut('fast');}是的,这也是正确的解决方案。在您的例子中,只需在closure.Awesome中使用recordToDelete变量。再次感谢您的帮助。:)
//When link with class .deleter is clicked, get the parent element's id (which is also that element's id in the database)
$('.deleter').click(function() {
    var item = $(this).closest('ul');
    var recordToDelete = item.attr('id');

//Send the id to the PHP script, which returns 1 if successful and 0 if not
    $.post("databasefuncs.php?func=delete", { postedMessage : recordToDelete },
        function(result){
            if(result == 1) {
                //Change the background color of the parent UL to red if successful
                item.css('background-color', 'red');
            } else {
                //Change the background color of the parent UL to blue if successful
                item.css('background-color', 'blue');
            }

        });
});