javascript - removeChild 导致错误

标签 javascript html

我有一个看起来像这样的表单:

    <form id="donate_form" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_cart">
        <input type="hidden" name="upload" value="1">
        <input type="hidden" name="business" value="">
        <input type="hidden" name="currency_code" value="EUR">
        <input type="hidden" name="return" value="www.google.com">
        <input type="hidden" name="custom" id="donate_custom_input" value="">
    </form>

我还有一个按钮,当按下该按钮时,它会调用一个向其添加 3 个新输入的函数。 所以在按下按钮后,表单看起来像这样:

    <form id="donate_form" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_cart">
        <input type="hidden" name="upload" value="1">
        <input type="hidden" name="business" value="">
        <input type="hidden" name="currency_code" value="EUR">
        <input type="hidden" name="return" value="www.google.com">
        <input type="hidden" name="custom" id="donate_custom_input" value="">
        <input type="hidden" data-formid="1" name="item_name_1" value="item1">
        <input type="hidden" data-formid="1" name="amount_1" value="5">
        <input type="hidden" data-formid="1" name="quantity_1" value="1">
    </form>

现在我正在尝试创建一个能够从表单中删除 3 个输入的按钮。

所以我写了下面的函数:

function removeFromForm(id)
{
    var formChilds = form.children; //Get array of children elements

    //formChildrenOffset = the amount of children in the form before the 3 new elements were added, so 6
    for(var i = formChildrenOffset;i < formChilds.length;i++)
    {
        if(formChilds[i].dataset.formid == id) //once we found an element with formID equal to the id of the elements we want to delete
        {
            for(var j = i; j < i+3; j++) //run a loop 3 times, to remove the 3 elements
                form.removeChild(formChilds[j]); //this seems to cause the error when j is 1

            break; //no need to go through the rest of the elements, so break;
        }
    }

}

问题是当我调用函数时出现以下错误:

   Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter        
   1 is not of type 'Node'.
       at removeFromForm (script.js:113)
       at RemoveFromCart (script.js:81)
       at HTMLButtonElement.onclick (shop.php:1)

表单最终看起来像这样:

    <form id="donate_form" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_cart">
        <input type="hidden" name="upload" value="1">
        <input type="hidden" name="business" value="rspslegendx-facilitator@gmail.com">
        <input type="hidden" name="currency_code" value="EUR">
        <input type="hidden" name="return" value="www.google.com">
        <input type="hidden" name="custom" id="donate_custom_input" value="">
        <input type="hidden" data-formid="1" name="amount_1" value="5">
    </form>

所以当它试图在第二个循环之后删除第二个元素时,似乎有什么东西导致了错误。

一段时间以来,我一直在试图弄清楚是什么原因造成的,但我似乎无法弄清楚。也许其他人可以发现我的错误?

最佳答案

假设你有这个数组[a,b,c,d,e]。你想删除 b,c,d 所以你做了一个从 1 到 3 的循环(b,c,d 的数组索引)并删除索引 1,然后删除索引2,然后是索引 3。但是当您删除索引 1 时,您的数组变为 [a,c,d,e] 因此删除索引 2 会留下 [a,c,e]然后删除索引 3 给出 error - there is no index 3

要解决此问题,请在删除元素时从您使用的迭代器中减去 1,或者向后迭代数组,或者继续删除相同的数组索引 j 次。

无论如何,这是删除表单字段的更简洁的方法(我将 3 个文本字段设为要删除的文本字段,所以很明显它们已经消失了)

function removeFromForm(id) {
  var f = document.querySelector("#donate_form");
  var inps = f.querySelectorAll("input[data-formid='" + id + "']");
  for (var i=inps.length - 1; i >= 0; i--) {
    f.removeChild(inps[i]);
  }
}
removeFromForm(1);
    <form id="donate_form" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
        <input type="hidden" name="cmd" value="_cart">
        <input type="hidden" name="upload" value="1">
        <input type="hidden" name="business" value="">
        <input type="hidden" name="currency_code" value="EUR">
        <input type="hidden" name="return" value="www.google.com">
        <input type="hidden" name="custom" id="donate_custom_input" value="">
        <input type="text" data-formid="1" name="item_name_1" value="item1">
        <input type="text" data-formid="1" name="amount_1" value="5">
        <input type="text" data-formid="1" name="quantity_1" value="1">
    </form>

关于javascript - removeChild 导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45534860/

相关文章:

javascript - 如何在javascript中获取当前年份的第一天?

html - 负 css 行未使用 100% svg 图像正确缩放

javascript - 从 json 动态生成复选框列表

javascript - 光滑 slider 多个 slider 保持事件位置

javascript - 强制网站将 'reload' 置于页面顶部

html - 使链接文本与链接背景高度相同

html - 为什么我不能覆盖用户样式代理对输入的填充?

html - 如何通过展开空标签来分层背景图片?

javascript - 使用 android 加速度计数据动画对象

javascript - 如何使 Bootstrap 4 列跟随页面滚动?