我只想每次删除一条新闻,但我;I’我删除了不止一个

I just want to delete one news each time, but I´m deleting more than one

本文关键字:删除 不止 一个 只想 一条 新闻 但我      更新时间:2023-09-26

我有一个php文件,其中包含来自数据库的新闻列表。

在每条新闻中,我都有一个链接可以删除它,就像这样:

echo '<a id="'.$result_news['id_news'].'" class="j_newsdelete" href="#">Delete</a>';

然后我有我的jQuery和AJAX来获取我的新闻id并删除我的新闻。

但只有当用户在我提供的对话框模式中单击"是"时,我的新闻才会被删除。

<div class="delete_dialog">
    <div class="confirm">
        <p></p>
        <a href="#" id="delete">Yes</a>
        <a href="#" id="no">No</a>
    </div>
</div>

当我删除每一条新闻时,我的删除操作都很好,但是。。。我这里有个问题。

我遇到的问题:

当我点击"删除"时,我的对话框确认出现,

但如果我在那一刻点击"否",新闻就不会被删除(所以工作正常),我的确认对话框也会关闭。。。

但接下来,如果我在其他新闻中单击"删除",并在对话框确认中单击"是",则此新闻将被删除,但我之前单击"否"的新闻也将被删除。

你能帮我了解一下这里发生了什么吗?

我的jQuery;Ajax

$('.content').on('click','.j_newsdelete',function(){
    var news_id = $(this).attr('id');
    $('.content .news li[id="'+ news_id +'"]').css('background','red');
    $('.delete_dialog p').text('Are you sure you want to remove this news?');
    $('.delete_dialog').fadeIn("slow",function(){
        $('.confirm').fadeIn("slow");
    });
    $("a#no").click(function(event){
        event.preventDefault();
        $('.confirm').fadeOut("slow",function(){
            $('.delete_dialog').fadeOut("slow"); 
        });
        $('.content .news li[id="'+ news_id +'"]').css('background','#f5f5f5');
    });
    $("a#delete").click(function(event){
        event.preventDefault();
        $.post(url,{action:'news_del',id: news_id},function(){
            window.setTimeout(function(){
                $('.content .news li[id="'+ news_id +'"]').fadeOut("slow");
        },500);
        $('.confirm').fadeOut("fast",function(){
            $('.delete_dialog').fadeOut("fast"); 
        });
    });
});
return false;
})

将事件绑定到外部,并将变量也放在外部。

var news_id = null;
$("a#no").click(function(event){
    event.preventDefault();
    $('.confirm').fadeOut("slow",function(){
        $('.delete_dialog').fadeOut("slow"); 
    });
    $('.content .news li[id="'+ news_id +'"]').css('background','#f5f5f5');
    news_id = null;
});
$("a#delete").click(function(event){
    event.preventDefault();
    if (!news_id) return;
    $.post(url,{action:'news_del',id: news_id},function(){
        window.setTimeout(function(){
            $('.content .news li[id="'+ news_id +'"]').fadeOut("slow");
        },500);
        $('.confirm').fadeOut("fast",function(){
            $('.delete_dialog').fadeOut("fast"); 
        });
    });
});
$('.content').on('click','.j_newsdelete',function(){
    news_id = $(this).attr('id');
    $('.content .news li[id="'+ news_id +'"]').css('background','red');
    $('.delete_dialog p').text('Are you sure you want to remove this news?');
    $('.delete_dialog').fadeIn("slow",function(){
        $('.confirm').fadeIn("slow");
    });
    return false;
});

事实上,您的问题是由于在其他事件处理程序中分配事件处理程序,特别是每次单击原始删除按钮时都绑定是/否单击处理程序。

基本上:

$('.content').on('click','.j_newsdelete',function(){
    ...
    $("a#no").click(function(event){
    ...
    });
    $("a#delete").click(function(event){
    ...
    });
 });

要更好地证明问题,请参阅以下内容:http://jsfiddle.net/6p8mx7nd/

一个可能的解决方案是为单击的新闻项目id使用全局变量:http://jsfiddle.net/6p8mx7nd/1/