有没有什么好的方法可以删除对应的选择表格?

使用此代码有一个按钮,您可以添加一个选择,这将添加 2 个输入框和一个带有 X 的按钮,当您按下 x 按钮时,我希望它删除相应的选择表单,给任何人一个好关于如何做的想法


 //adds more selection forms 

  var selectionAmmount = 0;

  var div;

  function addSelection() {

    var div = document.createElement('div')

    div.innerHTML = '<br>' + 

        '<input type="text" placeholder="Name Of selection">' + '<br>' +

        '<input type="number" placeholder="Price per s.y"">' + '<input type="button" value="x" onClick="removeSelection()">' + '<br>';


    document.getElementById("addSelection").appendChild(div);

    selectionAmmount++;

  }


  function removeSelection(t) {

    document.getElementById("addSelection").removeChild(document.getElementById("addSelection").childNodes[selectionAmmount - 1]);

    selectionAmmount--;

  }

 <div>

Has a selection: <input type="checkbox" id="selection" checked><br><br>

<selection id="hideSelection">

  <input type="text" placeholder="Name Of selection" id="selectionName"><br>

  <input type="number" placeholder="Price per s.y" id="selectionPrice"><br>

  <selection id="addSelection"></selection>   

  <input type="button" value="+ selection" onclick="addSelection()"><br><br>

</selection>

如果您想查看代码的实际运行情况,这是一个链接到是否托管代码:https ://ogdens-flooring-estimator.hunterscott1.repl.co/carpet.html


如果有帮助,我还可以发布整个文件,请告诉我,我将不胜感激任何帮助:)


不负相思意
浏览 135回答 1
1回答

摇曳的蔷薇

将id分配给您在addSelection()中创建的div&nbsp;div.id=`selection${id}`;&nbsp;在按钮 onclick中传递 idonClick="removeSelection(${id})"在removeSelection函数中,通过该id获取元素并将其删除。document.getElementById(`selection${id}`).remove();运行以下代码段。var selectionAmmount = 0;var id=1;var div;function addSelection() {&nbsp; var div = document.createElement('div')&nbsp; div.innerHTML = `<br><input type="text" placeholder="Name Of selection"><br><input type="number" placeholder="Price per s.y""><input type="button" value="x" onClick="removeSelection(${id})"><br>`;&nbsp; div.id=`selection${id}`;&nbsp; &nbsp;&nbsp;&nbsp; document.getElementById("addSelection").appendChild(div);&nbsp; selectionAmmount++;&nbsp; id++;}function removeSelection(id) {&nbsp;&nbsp; selectionAmmount--;&nbsp; document.getElementById(`selection${id}`).remove();}<div>Has a selection: <input type="checkbox" id="selection" checked><br><br><selection id="hideSelection">&nbsp; <input type="text" placeholder="Name Of selection" id="selectionName"><br>&nbsp; <input type="number" placeholder="Price per s.y" id="selectionPrice"><br>&nbsp; <selection id="addSelection"></selection>&nbsp; &nbsp;&nbsp; <input type="button" value="+ selection" onclick="addSelection()"><br><br></selection>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript