简体   繁体   English

如何添加动态表单元素但保留其值(JS)

[英]How do I add dynamic form elements but keep their values (JS)

How do I simply add dynamic form elements but keep their values? 如何简单地添加动态表单元素但保留其值? on adding an input using my current method it doesn't pass any info typed into the fields (deleted existing: see fiddle at the bottom) I think the problem is how I am adding the new inputs document.getElementById("add_ac").innerHTML += str; 使用我当前的方法添加输入时,它不会将键入的任何信息传递到字段中(删除现有信息:请参阅底部的小提琴)。我认为问题是我如何添加新输入document.getElementById("add_ac").innerHTML += str;

I have found other ways of adding dynamic form elements but they seem overly complicated does anyone know a very simple way of fixing the problem? 我发现了添加动态表单元素的其他方法,但它们似乎过于复杂,有人知道解决此问题的简单方法吗?

If you can't tell I don't code with JS or backend often, really just looking for a clean simple solution. 如果您不能告诉我我不经常使用JS或后端进行编码,那么实际上只是在寻找一种干净简单的解决方案。 Have looked on google and stack. 看过谷歌和堆栈。

Any real help would be great! 任何真正的帮助都将很棒!

<div id="add_ac">
  <input type="text" name="c[]" placeholder="Add comment..."/><br />
</div>
<button id="add_ac_btn">+</button>

<div id="add_ai">
  <input type="text" name="tai[]" placeholder="Add triggers, separated by commas"/> <input type="text" name="cai[]" placeholder="Add comment..."/><br />
</div>  
<button id="add_ai_btn">+</button>

JS JS

document.getElementById('add_ac_btn').onclick = function add_new_ac(){
  var str = '<input type="text" name="c[]" placeholder="Add comment..."/><br />';
  document.getElementById("add_ac").innerHTML += str;
}

document.getElementById('add_ai_btn').onclick = function add_new_ai(){
 var str = '<input type="text" name="tai[]" placeholder="Add triggers, separated by commas"/> <input type="text" name="cai[]" placeholder="Add comment..."/><br />';
document.getElementById("add_ai").innerHTML += str;


}

js fiddle: https://jsfiddle.net/fsdbpxoo/ js小提琴: https : //jsfiddle.net/fsdbpxoo/

Use insertAdjacentHTML as innerHTML += will reinsert all the DOM in the container. 使用insertAdjacentHTML作为innerHTML +=将所有DOM重新插入容器中。

insertAdjacentHTML() parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position. insertAdjacentHTML()将指定的文本解析为HTML或XML,并将结果节点插入到DOM树中的指定位置。 It does not reparse the element it is being used on and thus it does not corrupt the existing elements inside the element. 它不会重新解析正在使用该元素的元素,因此不会破坏该元素内部的现有元素。 This, and avoiding the extra step of serialization make it much faster than direct innerHTML manipulation. 这样,避免了额外的序列化步骤,使其比直接的innerHTML操作快得多。

Position "'beforeend'" : Just inside the element, after its last child. 位置“'beforeend'”在元素的最后一个子元素之后。

 document.getElementById('add_ac_btn').onclick = function add_new_ac() { var str = '<input type="text" name="c[]" placeholder="Add comment..."/><br />'; document.getElementById("add_ac").insertAdjacentHTML("beforeend", str); } document.getElementById('add_ai_btn').onclick = function add_new_ac() { var str = '<input type="text" name="tai[]" placeholder="Add triggers, separated by commas"/> <input type="text" name="cai[]" placeholder="Add comment..."/><br />'; document.getElementById("add_ai").insertAdjacentHTML("beforeend", str); } 
 <div id="add_ac"> <input type="text" name="c[]" placeholder="Add comment..." /> <br /> </div> <button id="add_ac_btn">+</button> <div id="add_ai"> <input type="text" name="tai[]" placeholder="Add triggers, separated by commas" /> <input type="text" name="cai[]" placeholder="Add comment..." /> <br /> </div> <button id="add_ai_btn">+</button> 

You need to append new elements instead of just replacing full content. 您需要添加新元素,而不仅仅是替换整个内容。 Use .appendChild() function for that: .appendChild()使用.appendChild()函数:

 document.getElementById('add_ac_btn').onclick = function add_new_ac() { var wrapper = document.getElementById('add_ac'); var newInput = document.createElement('input'); newInput.type = 'text'; newInput.name = 'c[]'; newInput.placeholder = 'Add comment...'; wrapper.appendChild(newInput); wrapper.appendChild(document.createElement('br')); } document.getElementById('add_ai_btn').onclick = function add_new_ac() { var wrapper = document.getElementById('add_ai'); var newInput = document.createElement('input'); newInput.type = 'text'; newInput.name = 'tai[]'; newInput.placeholder = 'Add triggers, separated by commas'; wrapper.appendChild(newInput); var newInput = document.createElement('input'); newInput.type = 'text'; newInput.name = 'cai[]'; newInput.placeholder = 'Add comment...'; wrapper.appendChild(newInput); wrapper.appendChild(document.createElement('br')); } 
 <div id="add_ac"> <input type="text" name="c[]" placeholder="Add comment..." /> <br /> </div> <button id="add_ac_btn">+</button> <div id="add_ai"> <input type="text" name="tai[]" placeholder="Add triggers, separated by commas" /> <input type="text" name="cai[]" placeholder="Add comment..." /> <br /> </div> <button id="add_ai_btn">+</button> 

Hi There use clone method to duplicate form element and insert in parent node. 您好,请使用克隆方法来复制表单元素并插入父节点中。 I made changes in your code... 我对您的代码进行了更改...

HTML 的HTML

<div id="add_ac">
  <div id="duplicater">
    <input type="text" name="c[]" placeholder="Add comment..."/>
  </div>
</div>
<button id="add_ac_btn">+</button>

<div id="add_ai">
  <div id="duplicetorSec">
    <input type="text" name="tai[]" placeholder="Add triggers, separated by commas"/> 
    <input type="text" name="cai[]" placeholder="Add comment..."/>
  </div>
</div>  
<button id="add_ai_btn">+</button>

JS CODE : JS代码:

<script>
var i = 0;
var k = 0;
var original = document.getElementById('duplicater');
var originalSec = document.getElementById('duplicetorSec');

function duplicate(){
  var clone = original.cloneNode(true); // "deep" clone
  i = ++i;
  clone.id = "duplicetor"+ i; // there can only be one element with  an ID
  original.parentNode.appendChild(clone);
  clearCloneElement(clone.id);
}
function duplicateSec(){
  var clone = originalSec.cloneNode(true); // "deep" clone
  console.log(clone);
  k = ++k;
  clone.id = "duplicetorSec"+ k; // there can only be one element with  an ID
  originalSec.parentNode.appendChild(clone);
  clearCloneElement(clone.id);
}
function clearCloneElement(id){ 
  var divId = '#'+id;
  $(divId).find("input[type^='text']").each(function() {
    $(this).val('');
  });
}
document.getElementById('add_ac_btn').onclick = function add_new_ac(){
  duplicate();
}
document.getElementById('add_ai_btn').onclick = function add_new_ac(){
    duplicateSec();
}
</script>

Please see here demo 请看这里演示

Must include JQuery file in header of page 页面标题中必须包含JQuery文件

您应该尝试使用JQuery的append()方法或采用普通JS的方式构造一个节点,然后使用appendChild()添加它。

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

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