【发布时间】:2017-06-23 09:33:37
【问题描述】:
我正在尝试使用多个“添加”按钮在同一页面上创建多个列表。
在文本字段 1 中输入文本并单击 button1 应该只会将内容添加到 list1(星期一)。但是 button1 正在将文本字段 2 中的文本添加到最后加载的 JS 中,即 list2 (星期二)。总共有 8 个列表,我只是想让前 2 个列表正常工作。
这是 JSFiddle:https://jsfiddle.net/sarwech/vrn5s2ns/4/
这看起来没有适当的关闭,我考虑过创建单独的本地函数,但我不太确定......
当我只想将项目添加到每个列表中时,以下方法有效:
document.getElementById("add1").onclick = function() {
var node = document.createElement("Li");
var text = document.getElementById("user_input1").value;
var textnode=document.createTextNode(text);
node.appendChild(textnode);
document.getElementById("list_item1").appendChild(node);
localStorage.setItem('monday', JSON.stringify(list_item1));
show();
return false; }
document.getElementById("add2").onclick = function() {
var node = document.createElement("Li");
var text = document.getElementById("user_input2").value;
var textnode=document.createTextNode(text);
node.appendChild(textnode);
document.getElementById("list_item2").appendChild(node); }
感谢任何帮助!
【问题讨论】:
标签: javascript list button