首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript待办事项列表

Javascript待办事项列表
EN

Stack Overflow用户
提问于 2017-02-21 04:35:13
回答 2查看 3.8K关注 0票数 0

我试图做一个待办事项列表应用程序,并试图添加一个可点击的复选框旁边的每一项,因为它是添加到列表。我对此非常陌生,所以任何帮助都将不胜感激!

谢谢!

代码语言:javascript
复制
   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)
    }   
代码语言:javascript
复制
  <form id="todoForm">
      <h1>To Do List:<h1>
        <input id="todoInput">
        <button type="button" onclick="todoList()">Add Item</button>
    </form>
    <ul id="todoList">
    </ul>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-21 04:46:46

更新Javscript

jsFiddle演示

代码语言:javascript
复制
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)
    }  
票数 2
EN

Stack Overflow用户

发布于 2017-02-21 04:48:11

我相信你想要添加复选框,而不是子弹。下面的代码可以做到这一点。如果您想了解有关创建"Todo“应用程序的更多信息,请从TodoMVC获得灵感。

代码语言:javascript
复制
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)
}
代码语言:javascript
复制
<!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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42358758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档