编程语言
首页 > 编程语言> > 如何使用.appendChild在纯JavaScript中添加包含多个文本输入的div(不带JQuery)

如何使用.appendChild在纯JavaScript中添加包含多个文本输入的div(不带JQuery)

作者:互联网

对于当前的实验,我正在尝试制作动态联系表单.目标是让用户只需单击按钮即可添加和删除地址,并指明其类型.

我注意到,当使用innerHTML表单时,已经包含的值会在使用新的添加或删除时丢失其值.这似乎是innerHTML的本质(this answer帮助我理解了问题是什么,并且还指向了appendChild).

但是我没有看到如何添加复杂元素,例如< div>包含多个文本输入和一个按钮.有没有办法实现与我正在做的相同,或防止其他形式失去输入值而不使用JQuery?

我试图为此创建一个MCVE,这里是一个JS小提琴,以节省时间.

工作的JS Fiddle在这里:JSFiddle

var targetOutput = document.getElementById("myForm");
var addressOutput = document.getElementById("addresses");
var requiredAddress = 1;

addressRemoveButton = `<input type="button" value="Remove" name="address" onclick="removeDiv(this)">`;
addressMolecule = `<div class="content"><label>Type:</label>&#9;&#9;<input type="text" name="address['type'][]"><br>
                <label>Address Line 1:</label><input type="text" name="address['line1'][]"><br>
                <label>Address Line 2:</label><input type="text" name="address['line2'][]"><br>
                <label>City:</label><input type="text" name="address['city'][]"><br>
                <label>State:</label><input type="text" name="address['state'][]"><br>
                <label>Zipcode:</label><input type="text" name="address['zipcode'][]"><br>`

window.onload = function(event) {
  myFun('window');
}

function myFun(a) {
  var i;
  for (i = 0; i < requiredAddress; i++) {
    addAddress(false);

    //alert("Max: " + maxAddress + "Current: " + addressCount);
  }
}

function removeDiv(e) {
  e.parentNode.removeChild(e.previousSibling);
  e.parentNode.removeChild(e);
}

function addAddress(removable = true) {
  addressOutput.innerHTML += addressMolecule;
  if (removable == true) {
    addressOutput.innerHTML += addressRemoveButton;
  }
  addressOutput.innerHTML += `</div>`;
}
<body onl oad="myFun('body')">
  <form name="myForm" id="myForm">
    <div id="addresses">
      <input type="button" id="addressButton" value="Add More" name="address" onclick="addAddress()">
    </div>
  </form>
</body>

解决方法:

如果要插入另一个HTML字符串而不使现有容器丢失其数据,请使用insertAdjacentHTML:

addressOutput.insertAdjacentHTML('beforeend', addressMolecule);
var targetOutput = document.getElementById("myForm");
var addressOutput = document.getElementById("addresses");
var requiredAddress = 1;

addressRemoveButton = `<input type="button" value="Remove" name="address" onclick="removeDiv(this)">`;
addressMolecule = `<div class="content"><label>Type:</label>&#9;&#9;<input type="text" name="address['type'][]"><br>
            <label>Address Line 1:</label><input type="text" name="address['line1'][]"><br>
            <label>Address Line 2:</label><input type="text" name="address['line2'][]"><br>
            <label>City:</label><input type="text" name="address['city'][]"><br>
            <label>State:</label><input type="text" name="address['state'][]"><br>
            <label>Zipcode:</label><input type="text" name="address['zipcode'][]"><br>`

window.onload = function(event) {
  myFun('window');
}

function myFun(a) {
  var i;
  for (i = 0; i < requiredAddress; i++) {
    addAddress(false);

    //alert("Max: " + maxAddress + "Current: " + addressCount);
  }
}

function removeDiv(e) {
  e.parentNode.removeChild(e.previousSibling);
  e.parentNode.removeChild(e);
}

function addAddress(removable = true) {
  addressOutput.insertAdjacentHTML('beforeend', addressMolecule);
  if (removable == true) {
    addressOutput.insertAdjacentHTML('beforeend', addressRemoveButton);
  }
  addressOutput.insertAdjacentHTML('beforeend', `</div>`);
}
<body onl oad="return myFun('body')">
  <form name="myForm" id="myForm">

    <div id="addresses">

      <input type="button" id="addressButton" value="Add More" name="address" onclick="addAddress()">
    </div>

  </form>
</body>

如您所述,使用appendChild可能会更好.创建一个div,用所需的HTML填充它,然后将其附加到容器:

function addAddress(removable = true) {
  const newDiv = document.createElement('div');
  newDiv.className = 'content';
  newDiv.innerHTML = addressMolecule; 
  addressOutput.appendChild(newDiv);
  if (removable) {
    const button = addressOutput.appendChild(document.createElement('button'));
    button.outerHTML = addressRemoveButton;
  }
}
var targetOutput = document.getElementById("myForm");
var addressOutput = document.getElementById("addresses");
var requiredAddress = 1;

addressRemoveButton = `<input type="button" value="Remove" name="address" onclick="removeDiv(this)">`;
addressMolecule = `<label>Type:</label>&#9;&#9;<input type="text" name="address['type'][]"><br>
            <label>Address Line 1:</label><input type="text" name="address['line1'][]"><br>
            <label>Address Line 2:</label><input type="text" name="address['line2'][]"><br>
            <label>City:</label><input type="text" name="address['city'][]"><br>
            <label>State:</label><input type="text" name="address['state'][]"><br>
            <label>Zipcode:</label><input type="text" name="address['zipcode'][]"><br>`

window.onload = function(event) {
  myFun('window');
}

function myFun(a) {
  var i;
  for (i = 0; i < requiredAddress; i++) {
    addAddress(false);

    //alert("Max: " + maxAddress + "Current: " + addressCount);
  }
}

function removeDiv(e) {
  e.parentNode.removeChild(e.previousSibling);
  e.parentNode.removeChild(e);
}

function addAddress(removable = true) {
  const newDiv = document.createElement('div');
  newDiv.className = 'content';
  newDiv.innerHTML = addressMolecule; 
  addressOutput.appendChild(newDiv);
  if (removable) {
    const button = addressOutput.appendChild(document.createElement('button'));
    button.outerHTML = addressRemoveButton;
  }
}
<body onl oad="return myFun('body')">
  <form name="myForm" id="myForm">

    <div id="addresses">

      <input type="button" id="addressButton" value="Add More" name="address" onclick="addAddress()">
    </div>

  </form>
</body>

标签:javascript,dom-manipulation
来源: https://codeday.me/bug/20190627/1303737.html