HTML Javascript动态添加和删除文本框

wj8zmpe1  于 2023-04-04  发布在  Java
关注(0)|答案(2)|浏览(160)

我尝试使用javascript和HTML动态添加和删除文本框。我可以让它添加和删除,但有时删除按钮不起作用。当我检查元素时,它说删除按钮没有onclick值。我不明白为什么当我在添加函数中设置onclick时。
这里是我的代码:

<div  id="reqs">
<h3 align = "center"> Requirements </h3>
<script>
    var reqs_id = 0;
    function removeElement(elementId,elementId2) {
        // Removes an element from the document
        var element2 = document.getElementById(elementId2);
        var element = document.getElementById(elementId);
        element2.parentNode.removeChild(element2);
        element.parentNode.removeChild(element);

    }   
    function add() {
        reqs_id++;// increment reqs_id to get a unique ID for the new element

        //create textbox
        var input = document.createElement('input');
        input.type = "text";
        input.setAttribute("class","w3-input w3-border");
        input.setAttribute('id','reqs'+reqs_id);
        var reqs = document.getElementById("reqs");
        //create remove button
        var remove = document.createElement('button');
        remove.setAttribute('id','reqsr'+reqs_id);
        remove.onclick = function() {removeElement('reqs'+reqs_id,'reqsr'+reqs_id);return false;};
        remove.setAttribute("type","button");
        remove.innerHTML = "Remove";
        //append elements
        reqs.appendChild(input);
        reqs.appendChild(remove);
    }

</script>
<button type="button" value="Add" onclick="javascript:add();"> Add</button>
ddhy6vgd

ddhy6vgd1#

这将工作:

var reqs_id = 0;

function removeElement(ev) {
  var button = ev.target;
  var field = button.previousSibling;
  var div = button.parentElement;
  div.removeChild(button);
  div.removeChild(field);
}

function add() {
  reqs_id++; // increment reqs_id to get a unique ID for the new element

  //create textbox
  var input = document.createElement('input');
  input.type = "text";
  input.setAttribute("class", "w3-input w3-border");
  input.setAttribute('id', 'reqs' + reqs_id);
  input.setAttribute('value', reqs_id);
  var reqs = document.getElementById("reqs");
  //create remove button
  var remove = document.createElement('button');
  remove.setAttribute('id', 'reqsr' + reqs_id);
  remove.onclick = function(e) {
    removeElement(e)
  };
  remove.setAttribute("type", "button");
  remove.innerHTML = "Remove" + reqs_id;
  //append elements
  reqs.appendChild(input);
  reqs.appendChild(remove);
}
<div id="reqs">
  <h3 align="center"> Requirements </h3>
</div>
<button type="button" value="Add" onclick="javascript:add();"> Add</button>
j91ykkif

j91ykkif2#

从我以前的答案修正。另一个可能是必要的选项是让每个元素知道它的确切位置,并能够根据添加或删除的内容调整自己。此增强将通过重新调整并确保您的元素始终处于有序状态来解决这个问题。(如果需要)
参见JSFiddle示例。
HTML

<div id="reqs">
   <h3>Requirements</h3>
   <button type="button" value="Add" onclick="javascript:add();">Add</button>
   <br>
</div>

简体中文

function removeElement(e) {
    let button = e.target;
    let field = button.previousSibling;
    let div = button.parentElement;
    let br = button.nextSibling;
    div.removeChild(button);
    div.removeChild(field);
    div.removeChild(br);

    let allElements = document.getElementById("reqs");
    let inputs = allElements.getElementsByTagName("input");
    for(i=0;i<inputs.length;i++){
        inputs[i].setAttribute('id', 'reqs' + (i+1));
        inputs[i].setAttribute('value', (i+1));
        inputs[i].nextSibling.setAttribute('id', 'reqsr' + (i+1));
    }
}

function add() {
    let allElements = document.getElementById("reqs");
    let reqs_id = allElements.getElementsByTagName("input").length;
    reqs_id++;

    //create textbox
    let input = document.createElement('input');
    input.type = "text";
    input.setAttribute("class", "w3-input w3-border");
    input.setAttribute('id', 'reqs' + reqs_id);
    input.setAttribute('value', reqs_id);
    let reqs = document.getElementById("reqs");

    //create remove button
    let remove = document.createElement('button');
    remove.setAttribute('id', 'reqsr' + reqs_id);
    remove.onclick = function(e) {
        removeElement(e);
    };
    remove.setAttribute("type", "button");
    remove.innerHTML = "Remove";

    //append elements
    reqs.appendChild(input);
    reqs.appendChild(remove);
    let br = document.createElement("br");
    reqs.appendChild(br);
}

相关问题