【发布时间】:2016-12-19 10:06:56
【问题描述】:
当用户单击复选框时,我正在尝试删除复选框按钮旁边的文本。但是当我测试它时,由于某种原因什么都没有发生。我想检查该框是否已选中。如果是,那么我想在该复选框旁边越过该项目。但是,此功能不起作用。有人可以解释我做错了什么吗?谢谢。
function myFunction() {
var editButton = document.createElement("button");
//button.delete
var deleteButton = document.createElement("button");
var item = document.getElementById("todoInput").value
var checkBox = document.createElement("input");
checkBox.type = "checkbox";
checkBox.id = "checkbox"
var text = document.createTextNode(item)
var newItem = document.createElement("li")
newItem.className = "addedClass"
newItem.appendChild(text)
if (item === "") {
alert("please fill in the blanks");
} else {
var crap = document.getElementById("todoList")
crap.appendChild(newItem)
var addhere = document.getElementById("todoList")
addhere.appendChild(checkBox);
}
function updateItem() {
if (document.getElementById(checkbox).checked) {
document.getElementById(todoList).style.textDecoration = "line-through"
}
}
}
<form name="myForm" id="todoForm">
<input id="todoInput" name="fname" required>
<button type="button" onclick="myFunction()">OK</button>
</form>
<ol id="todoList"></ol>
【问题讨论】:
-
您似乎在“todoList”这个词周围缺少了一些引号——它说 document.getElementById(todoList)
-
试着想想当复选框被点击时会发生什么。目前,您的代码似乎无法处理事件。所以你要解决的第一个问题就是这个。还可以尝试使用开发者工具 (F12) 和控制台。
标签: javascript jquery checkbox