Jquery .each, remove $(this)?

3
假设有一个 jQuery 的 .each 循环:
function reverifyDiscounts() {
    //everything we need to verify a discount is already on the page. 
    //we'll remove the "bad" discounts when we submit the page
    console.info("Entering reverification");
    //event discounts are always valid once they're on the page because it's for the event
    $(".discountPromoItem").each(function () {
        //skip events
        if ($(this).attr("appliesto") == $("#hdnEventID").val()) {
            return true;
        }
        //we just need to make sure that the checkbox that the appliesto attribute references is checked!
        if (!$("checkbox[attribute$='" + $(this).attr("applitesto") + "']").is(":checked")) {
            //we also need to remove the promo code from the list of promo codes entered into the hidden textboxes
            $("#hdnAppliedPromoCode").val($("#hdnAppliedPromoCode").val().replace($(this).attr("code"), ""));
            //the item that it applies to is no longer selected and the promo must be removed
            $(this).remove(); //can't remove $(this) while inside the loop for whatever reason.
        }
    });
    recalculate();
}

为什么$(this).remove()失败了,或者我做错了什么?

3
文档中一个特定的ID只能对应于一个元素。 - Denys Séguret
1
请在 fiddle 中重现您的问题。 - Denys Séguret
我创建了一个简化版本的 jsfiddle,链接是 http://jsfiddle.net/pwm3h233/1/,并且它能正常运行。 - user4593252
@MetalPhoenix,也许你的问题是因为如果前一个条件为真就返回true。但是,没有看到你的问题,很难说出错了什么。无论如何,似乎很容易调试。 - A. Wolff
已添加了另一种方法,使用 filter 然后再使用 remove。你可以试试吗? - iCollect.it Ltd
显示剩余10条评论
1个回答

6

更新:

除了结尾缺少)之外,你的代码实际上是可以工作的:http://jsfiddle.net/TrueBlueAussie/hdc9ke9k/

问题一定在if测试中。

尝试使用过滤器然后执行删除最后一个操作:

function reverifyDiscounts() {
    //everything we need to verify a discount is already on the page. 
    //we'll remove the "bad" discounts when we submit the page
    console.info("Entering reverification");
    //event discounts are always valid once they're on the page because it's for the event
    $(".discountPromoItem").filter(function () {
        //skip events
        if ($(this).attr("appliesto") == $("#hdnEventID").val()) {
            return false;
        }
        //we just need to make sure that the checkbox that the appliesto attribute references is checked!
        if (!$("checkbox[attribute$='" + $(this).attr("applitesto") + "']").is(":checked")) {
            $("#hdnAppliedPromoCode").val($("#hdnAppliedPromoCode").val().replace($(this).attr("code"), ""));
            return true;
        }
    }).remove();

    recalculate();
}

问题原始版本的原始代码:

使用ID选择器的each没有意义,因为ID必须是唯一的,只有第一个匹配项会生效。

这是因为浏览器维护了一个高速查找字典,每个ID对应单个DOM元素。jQuery(和JavaScript)只能通过ID获取第一个匹配项。

对于多个匹配项,请改用类:

$(".thatThing").each(function(){ 
    if (someBoolCondition){
        $(this).remove(); 
    }

});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/hdc9ke9k/

这是一个JSFiddle链接,可以让你在网页上编辑和运行HTML、CSS和JavaScript代码。

这就是我实际在做的事情,我出于习惯输入了id选择器。我已经更正了代码以纠正这个问题。 - user4593252
在这种情况下,您的代码是正确的,因此错误可能在“if”测试中。您能展示其余真实代码吗? - iCollect.it Ltd
我已经放入了我正在使用的实际代码,应该能够突出问题。有趣的是,我尝试在一个简化的形式中制作这个 fiddle,它可以工作…… - user4593252
不要在循环内使用 each 并删除,而是使用 filter 查找所有需要删除的元素,然后将删除操作应用于返回的结果(true 的元素将被 filter 保留)。 - iCollect.it Ltd
还是一样,感谢你的帮助。但愿我知道为什么。另外,我今天学到了.filter的用法。 :) - user4593252
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接