使用 js / jQuery 从表单中删除动态创建的子节点?

发布于 2024-09-14 00:01:42 字数 722 浏览 2 评论 0原文

下面的帖子对我想做的事情非常有帮助: 用 PHP 构建包含多个商品和 PayPal 按钮的购物车

这部分工作得很漂亮:

    var inp1 = document.createElement("input");
    inp1.setAttribute("type", "hidden");
    inp1.setAttribute("id", "item_number_" + current_item);
    inp1.setAttribute("name", "item_number_" + current_item);
    inp1.setAttribute("value", current_item);

    document.getElementById("paypal-form").appendChild(inp1);

但我对如何在需要时删除项目有点困惑...我正在寻找类似的东西:

    document.getElementById('payPalForm').removeChild(inp1);

但显然我需要一种方法来指定/跟踪那些动态创建的 id...或者是否有一个我失踪的更简单的方法?

任何帮助将不胜感激。

The below post was hugely helpful for what I'm trying to do:
Builing a cart with multiple items and the PayPal button in PHP

This part works beautifully:

    var inp1 = document.createElement("input");
    inp1.setAttribute("type", "hidden");
    inp1.setAttribute("id", "item_number_" + current_item);
    inp1.setAttribute("name", "item_number_" + current_item);
    inp1.setAttribute("value", current_item);

    document.getElementById("paypal-form").appendChild(inp1);

but I'm getting a bit stuck on how to remove an item when needed... I am looking for something like:

    document.getElementById('payPalForm').removeChild(inp1);

But obviously I need a way to specify/track those dynamically created id's... or is there an easier way I'm missing?

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒与心事 2024-09-21 00:01:42

使用parentNode属性检索元素的父元素,并使用父元素的removeChild方法:

inp1.parentNode.removeChild (inp1);

参考:
parentNode 属性

use the parentNode property to retrieve the parent element of an element, and use the removeChild method of the parent:

inp1.parentNode.removeChild (inp1);

Reference:
parentNode property

丢了幸福的猪 2024-09-21 00:01:42

我不确定这段代码属于哪里,但是将其封装在一个类中会很有帮助。

function NewClass() {
    var formPP = document.getElementById('payPalForm');
    var lInputs = [];
    this.addItem = function(val) {
        var inp1 = document.createElement("input");
        inp1.setAttribute("type", "hidden");
        inp1.setAttribute("id", "item_number_" + val);
        inp1.setAttribute("name", "item_number_" + val);
        inp1.setAttribute("value", val);
        formPP.appendChild(inp1);
        lInputs.push(inp1);
    }
    this.removeItem = function(val) {
        var e = formPP.firstChild;
        while( e ) {
            var eNext = e.nextSibling;
            if( e.value == val )
                formPP.removeChild(e);
            e = eNext;
        }
    }
}

您必须对其进行定制以满足您的需求,但希望这能给您一个开始。

I'm not sure where this code belongs, but it would be helpful to encapsulate this within a class.

function NewClass() {
    var formPP = document.getElementById('payPalForm');
    var lInputs = [];
    this.addItem = function(val) {
        var inp1 = document.createElement("input");
        inp1.setAttribute("type", "hidden");
        inp1.setAttribute("id", "item_number_" + val);
        inp1.setAttribute("name", "item_number_" + val);
        inp1.setAttribute("value", val);
        formPP.appendChild(inp1);
        lInputs.push(inp1);
    }
    this.removeItem = function(val) {
        var e = formPP.firstChild;
        while( e ) {
            var eNext = e.nextSibling;
            if( e.value == val )
                formPP.removeChild(e);
            e = eNext;
        }
    }
}

You'll have to tailor this to meet your needs, but hopefully this gives you a start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文