简体   繁体   English

使用 innerHTML 连接 HTML 时防止重新加载 HTML 元素

[英]Prevent HTML element reload when concatenating HTML using innerHTML

I have a form having two input fields:我有一个包含两个输入字段的表单:

<form id="import-products-form">
  <div class="form-row">
    <select></select>
    <input>
  </div>
</form>

And a button:还有一个按钮:

<button id="add-input-button"><strong>+</strong></button>

Everytime the button is clicked, two input fields will be added to the form:每次单击按钮时,表单中都会添加两个输入字段:

document.getElementById("add-input-button").onclick = () => {
  const input = `
    <div class="form-row">
      <select></select>
      <input>
    </div>
  `;

  document.getElementById("import-products-form").innerHTML += input;
};

The problem here is whenever the button is clicked, the values of the existed fields will be reset to default.这里的问题是每当单击按钮时,现有字段的值将重置为默认值。 In DevTools , I saw that the entire form was reloaded when the button clicked.DevTools ,我看到当单击按钮时整个表单被重新加载。 Is there anyway to keep the values of the existed fields when new fields added to the form?当新字段添加到表单时,是否要保留现有字段的值?

Don't assign to innerHTML .不要分配给innerHTML That causes all the elements inside the form to be recreated from scratch, and any dynamic state is lost.这会导致表单内的所有元素从头开始重新创建,并且任何动态状态都将丢失。

Use insertAdjacentHTML instead.请改用insertAdjacentHTML This parses the new HTML and appends the DOM elements, without disturbing the original elements.这会解析新的 HTML 并附加 DOM 元素,而不会干扰原始元素。

document.getElementById("import-products-form").insertAdjacentHTML('beforeend', input);

x.innerHTML += input effectively runs x.innerHTML = (x.innerHTML + input) and you lose the state x.innerHTML += input有效地运行x.innerHTML = (x.innerHTML + input)并且你失去状态


You should create new element and append it.您应该创建新元素并附加它。

 document.getElementById("add-input-button").onclick = (e) => { const input = ` <select></select> <input> `; let div = document.createElement('div') div.classList.add('form-row') div.innerHTML=input document.getElementById("import-products-form").appendChild(div) };
 <form id="import-products-form"> <div class="form-row"> <select></select> <input> </div> </form> <button id="add-input-button"><strong>+</strong></button>


For this specific use case, no need to create element from text every time.对于此特定用例,无需每次都从文本创建元素。

 { const input = ` <select></select> <input> `; let div = document.createElement('div') div.classList.add('form-row') div.innerHTML=input document.getElementById("add-input-button").onclick = (e) => { document.getElementById("import-products-form").appendChild(div.cloneNode(true)) }; }
 <form id="import-products-form"> <div class="form-row"> <select></select> <input> </div> </form> <button id="add-input-button"><strong>+</strong></button>

Try using appendChild instead of innerHTML .尝试使用appendChild而不是innerHTML

 document.getElementById("add-input-button").onclick = () => { var div = document.createElement('div'); var select = document.createElement('select'); var input = document.createElement('input'); div.classList.add('form-row'); div.appendChild(select); div.appendChild(input); document.getElementById("import-products-form").appendChild(div); };
 <form id="import-products-form"> <div class="form-row"> <select></select> <input> </div> </form> <button id="add-input-button"><strong>+</strong></button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM