我试图做一个待办事项列表应用程序,并试图添加一个可点击的复选框旁边的每一项,因为它是添加到列表。我对此非常陌生,所以任何帮助都将不胜感激!
谢谢!
function todoList() {
var item = document.getElementById('todoInput').value
var text = document.createTextNode(item)
var newItem = document.createElement("li")
newItem.appendChild(text)
document.getElementById("todoList").appendChild(newItem)
} <form id="todoForm">
<h1>To Do List:<h1>
<input id="todoInput">
<button type="button" onclick="todoList()">Add Item</button>
</form>
<ul id="todoList">
</ul>
发布于 2017-02-21 04:46:46
更新Javscript
function todoList() {
var item = document.getElementById('todoInput').value
var text = document.createTextNode(item)
var newItem = document.createElement("li")
newItem.appendChild(text)
var checkbox = document.createElement('input');
checkbox.type = "checkbox";
checkbox.name = "name";
checkbox.value = "value";
checkbox.id = "id";
newItem.appendChild(checkbox);
document.getElementById("todoList").appendChild(newItem)
} 发布于 2017-02-21 04:48:11
我相信你想要添加复选框,而不是子弹。下面的代码可以做到这一点。如果您想了解有关创建"Todo“应用程序的更多信息,请从TodoMVC获得灵感。
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("div");
newItem.appendChild(checkbox);
newItem.appendChild(text);
document.getElementById("todoList").appendChild(newItem)
}<!DOCTYPE html>
<html>
<body>
<form id="todoForm">
<h1>To Do List:<h1>
<input id="todoInput">
<button type="button" onclick="todoList()">Add Item</button>
</form>
<div id="todoList">
</div>
</body>
</html>
https://stackoverflow.com/questions/42358758
复制相似问题