使用 js / jQuery 从表单中删除动态创建的子节点?
下面的帖子对我想做的事情非常有帮助: 用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用parentNode属性检索元素的父元素,并使用父元素的removeChild方法:
参考:
parentNode 属性
use the parentNode property to retrieve the parent element of an element, and use the removeChild method of the parent:
Reference:
parentNode property
我不确定这段代码属于哪里,但是将其封装在一个类中会很有帮助。
您必须对其进行定制以满足您的需求,但希望这能给您一个开始。
I'm not sure where this code belongs, but it would be helpful to encapsulate this within a class.
You'll have to tailor this to meet your needs, but hopefully this gives you a start.