2017-02-21 113 views
0

我正在嘗試做一個待辦事項列表應用程序,並試圖在每個項目旁邊添加一個可單擊的複選框,因爲它已添加到列表中。我對此很新,所以任何幫助將不勝感激!Javascript To Do List

謝謝!

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> 
 

 

+1

是什麼問題? –

+1

看來您已經知道如何創建和追加一般意義上的元素(您所展示的代碼有效),那麼當涉及到複選框時,會給您帶來什麼麻煩? (你想要新的li元素中的複選框,對嗎?創建一個並追加到'newItem'中。) – nnnnnn

+0

我想我不確定需要添加的東西,例如類型,名稱和值等。謝謝對於回覆 – bemon

回答

1

更新Javscript

jsFiddle Demo

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) 
    } 
+0

謝謝我欣賞評論和示例的幫助。 – bemon

0

我相信你想添加的,而不是子彈複選框。下面的代碼就是這樣做的。如果您想了解更多關於創建「待辦事項列表」的應用程序,然後採取的靈感來自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>

+0

謝謝,我感謝你的幫助。我很快就會看到這個鏈接。再次感謝。 – bemon