【发布时间】:2017-02-22 06:57:16
【问题描述】:
我正在尝试将用户输入添加到数组中。我似乎无法弄清楚如何去做。我花了几个小时试图弄清楚到底该怎么做。我还希望将这个“待办事项列表”保存到我的本地存储中,我同样对试图找出这两个问题感到沮丧。
任何关于如何从用户输入添加到阵列和/或如何将其放入本地存储的建议或指导将不胜感激。到目前为止,我花了相当长的时间。感谢您的所有帮助!非常感谢。
Javascript
var theList = [];
function todoList() {
var item = document.getElementById('todoInput').value;
var text = document.createTextNode(item);
var checkbox = document.createElement('input');
checkbox.type = "checkbox";
checkbox.name = "name";
checkbox.value = "value";
var newItem = document.createElement("li");
newItem.appendChild(checkbox);
newItem.appendChild(text);
document.getElementById("todoList").appendChild(newItem)
return clear();
}
function clear() {
todoInput.value = "";
}
console.log(theList);
HTML
<h1>To Do List:<h1>
<input id="todoInput" type="text">
<button type="button" onclick="todoList()">Add Item</button>
</form>
<ol id="todoList">
</ol>
<script src="todo.js"></script>
【问题讨论】:
-
你介意用 jQuery 代替 JavaScript 吗?
-
我还没有开始学习 jQuery,但感谢您的帖子!我将不得不通过它来做出正面/反面,但谢谢......这是一个很好的参考点!
标签: javascript html arrays local-storage push