对于当前的实验,我正在尝试制作动态联系表格。目的是让用户只需单击按钮即可添加和删除地址,并指示其类型。
我注意到,在使用innerHTML
表单时,只要使用新的添加或删除操作,这些表单中已包含值的值就会丢失。这似乎是innerHTML
的本质(this answer帮助我理解了问题所在,并且还向我指出了appendChild
)。
但是,我看不到如何添加复杂的元素,例如包含多个文本<div>
和inputs
的{{1}}。有什么方法可以达到与我正在执行的操作相同的目的,或者防止其他形式在不使用JQuery的情况下丢失输入值?
我尝试为此创建一个MCVE,在这里,它与JS小提琴一起以节省时间。
有效的JS小提琴在这里:JSFiddle
button
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>		<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>`;
}
答案 0 :(得分:3)
如果要插入另一个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>		<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 onload="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>		<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 onload="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>