简体   繁体   English

使用输入javascript动态添加和删除列表组

[英]dynamic add and remove list group with input javascript

I am quite new to javascript and was wondering if anyone can help with an issue I am having.我对 javascript 很陌生,想知道是否有人可以帮助解决我遇到的问题。 I want to dynamically add and remove from a list group with inputs.我想从带有输入的列表组中动态添加和删除。 I have worked out the mechanic's from a script floating around the internet but having issues with getting the input as part of the add remove method.我已经从互联网上流传的脚本中找出了机制,但是在将输入作为 add remove 方法的一部分时遇到了问题。 see below.见下文。

<script type="text/javascript">
function addItem(){
  var ul = document.getElementById("selectme-list");
  var selectme= document.getElementById("selectme");
  var li = document.createElement("li");
  li.className = "list-group-item";
  li.setAttribute('id',selectme.value);
  li.setAttribute('name','codeal[]');
  li.setAttribute('value',selectme.value);
  li.appendChild(document.createTextNode(selectme.value));
  ul.appendChild(li);
}

function removeItem(){
    var ul = document.getElementById("selectme-list");
  var selectme = document.getElementById("selectme");
  var item = document.getElementById(selectme.value);
  ul.removeChild(item);
}
</script>

This script adds and removes the LI fine.此脚本添加和删除 LI 罚款。 But I want to have the input hold the information.但我想让输入保存信息。

I want example: <li class="list-group-item"><input id="RM" name="codeal[]" value="RM"/>RM</li> Currently getting example: <li class="list-group-item" id="RM" name="codeal[]" value="RM">RM</li>我想要示例: <li class="list-group-item"><input id="RM" name="codeal[]" value="RM"/>RM</li>目前正在获取示例: <li class="list-group-item" id="RM" name="codeal[]" value="RM">RM</li>

Any help would be much appreciated thanks任何帮助将不胜感激谢谢

Try creating a new input node as you created list and append it to list node as child element.尝试在创建列表时创建一个新的input节点,并将其作为子元素附加到列表节点。

function addItem(){
  var ul = document.getElementById("selectme-list");
  var selectme= document.getElementById("selectme");
  var li = document.createElement("li");
  li.className = "list-group-item";
  li.setAttribute('id',selectme.value);

  x = document.createElement("INPUT");
  x.setAttribute('name','codeal[]');
  x.setAttribute("type", "text");
  x.setAttribute("value", selectme.value);
  li.appendChild(x);
  ul.appendChild(li);
} 

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

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