AJAX 删除 - 使用 jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/375356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:03:01  来源:igfitidea点击:

AJAX delete - using jQuery

jqueryajax

提问by Coughlin

I have a simple cart page that displays items that are in someones cart, and having it display via an ASP while from my table. I have a column where a user can delete an entry. I have the ASP working properly, now I am trying to add some AJAX in to it. I have the following code:

我有一个简单的购物车页面,它显示某人购物车中的项目,并在我的桌子上通过 ASP 显示它。我有一个列,用户可以在其中删除条目。我的 ASP 工作正常,现在我正在尝试向其中添加一些 AJAX。我有以下代码:

$("img.delete").click(function() {
var id     = $('#id').attr('value');        
    $.ajax({
        type: "POST",
        url: "delete.php",
        data: "id="+ id,
        success: function(){
            $('tr.selector').remove();
            $('div.success').fadeIn();
        }
    });
return false;
});

The thing is, how wouild I go about setting it up for each value, because if I use the above, I click one and they will all go. I am confused on how to set it up to work with numerous rows.

问题是,我将如何为每个值设置它,因为如果我使用上面的,我点击一个,它们都会消失。我对如何设置它以处理多行感到困惑。

回答by Eran Galperin

You need to select only the item's row for removal. I'm not sure how you have it set up, but if the image element is inside the row you could use:

您只需选择要删除的项目所在的行。我不确定你是如何设置的,但如果图像元素在行内,你可以使用:

 $("img.delete").click(function() {
      var row = $(this).parents('tr:first');

      ...

      success: function(){
           $(row).remove(); //Remove the row containing the image element
           ...
      }

      ...
  });

回答by finpingvin

If it is the html id-attribute you want, then that would work. Why don't you try it?

如果它是您想要的 html id-attribute,那么就可以了。你为什么不试试呢?

EDIT: It might be just row.attr('id'); It have slipped from my mind, havn't ben using jQuery for sometime :)

编辑:它可能只是 row.attr('id'); 它已经从我的脑海中消失了,有一段时间没有使用 jQuery 了 :)

回答by ely

The simple thing is to attach a $variable (id)to the class of each item, say

简单的事情是将一个附加$variable (id)到每个项目的类,比如

// $("img.delete_<?php echo $id;?>").click(function()

in this case it appears as img.delete_1, img.delete_2 ect for each item,

在这种情况下,对于每个项目,它显示为 img.delete_1、img.delete_2 等,

then apply the same to the class of the looping item

然后将相同的应用于循环项的类

<td class="delete_<?php echo $id;?>"></td>

hope it makes sense

希望这是有道理的

$("img.delete_<?php echo $id;?>").click(function() {
var id     = $('#id').attr('value');            
        $.ajax({
                type: "POST",
                url: "delete.php",
                data: "id="+ id,
                success: function(){
                        $('tr.selector').remove();
                        $('div.success').fadeIn();
                }
        });
return false;
});