如何删除jQuery的AJAX调用后添加的元素? [英] How to remove element added after AJAX call in jQuery?

查看:111
本文介绍了如何删除jQuery的AJAX调用后添加的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的试图删除HTML元素在从一个AJAX调用返回添加到我的网页。 但是,jQuery的似乎不知道这种动态地添加元素。任何解决办法?

I'm trying to remove an HTML element added to my page upon return from an AJAX call. But jQuery seems to be unaware of such dynamically added elements. Any solutions?

上下文

  • 在我建立一个简单的购物车。
  • 当用户点击添加按钮,我在数据库中添加的项目,我想在屏幕上显示它无需刷新页面。
  • 但是,因为用户可能会点击添加多次为同一项目 在购物会话,每次他点击的时候,我想删除任何 线路从购物车DIV的项目,只保留最 最近的一个用于该项目。
  • I'm building a simplistic shopping cart.
  • When the user clicks the 'Add' button, I add the item in the database AND I want to display it onscreen without reloading the page.
  • But since the user might click 'Add' multiple times for the same item in a shopping session, each time he clicks, I want to remove any lines of that item from the shopping cart DIV, and keep only the most recent one for that item.

现在,这是行不通的,所以我的HTML看起来像这样多后,补充道:

Right now, it doesn't work so my HTML look like this after multiple adds:

<div id="CartList">
  <p id="Item_shirt7363">shirt7363</p>
  <p id="Item_shirt7363">shirt7363</p>
  <p id="Item_shirt7363">shirt7363</p>
</div>

我的jQuery的是这样的:

My jQuery looks like this:

function AddToCartClicked()
{
    item_code = $("#ItemCode").val();
    qty = $("#Qty").val(); 

    $.ajax
        (
            {
                type: "POST",
                url: "/_res/php/add-item.php",
                dataType: "text",
                data: "puid=123&code=" + item_code + "&qty=" + qty,
                success: function(data)
                {       
                    // PROBLEM: First, remove any existing line with this ID
                    $("#Item_" + item_code).remove();               

                    // Write new line
                    newLine = "<p id='Item_" + item_code + "'>" + item_code + "</p>";                   

                    // Add the line in the HTML
                    $("#CartList").prepend(newLine);
                }
            }
        );

    return true;
}

我搜索了论坛,并没有找到一个匹配我的问题。此外,我在这个新的,所以请假设愚蠢。谢谢。

I searched the forums and did not find a match for my question. Also, I'm new at this, so please assume stupidity. Thanks.

推荐答案

感谢作为您的帮助家伙,它肯定帮我调试这个!

Thanks for all your help guys, it definitely helped me debug this!

在最后,问题是,有些项目codeS有一个时期,他们(shirt235.b)和jQuery有一个问题检索,如果期间没有逃脱( <一href="http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_select_an_element_by_an_ID_that_has_characters_used_in_CSS_notation.3F"相对=nofollow>这是在常见问题解答。)。

In the end, the issue was that some item codes had a period in them ("shirt235.b") and jQuery has a problem retrieving that if the period is not escaped (It's in the FAQ.).

我真的很抱歉,我用一个虚构的例子为简单起见,认为它并不重要什么ITEM_ code是......我吸取了教训,将从现在开始后真实世界的code!请接受我的道歉和感谢您的理解。

I'm really sorry that I used a fictional example for simplicity, thinking it didn't matter what the item_code was... I've learned my lesson and will only post real-world code from now on! Please accept my apologies and thanks for your understanding.

有一个伟大的一周!

这篇关于如何删除jQuery的AJAX调用后添加的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆