1

我对 Javascript 非常陌生,因此欢迎任何和所有输入。我在创建用于删除选定复选框的函数时遇到问题。我尝试了很多东西,但最终只是删除了整个功能。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Shopping List</title>
        <script>
            /*
            Create a shopping list program using javascript.  The page should have a text input, an “add” button, a “delete checked” button, and a list of items.  Each item should have a checkbox next to it.  If you fill out the text input and click” add,” it should add a list item to the end of the list.  If you check a box (or boxes) next to a list item then click “delete checked,” it should remove the list items that were checked.  Style the page using CSS.  
            */
            function addIt() {
                var list = document.getElementById("list"); //used to grab the parent level "list"
                var li = document.createElement("li"); //creates a "li" in the document
                var userTxt = document.getElementById("product"); //used to grab the user text input "product"
                var childText = userTxt.value; //returns the userTxt value and sets it equal to childText
                var childTextNode = document.createTextNode(childText); //creates a textNode in the document based on what the user entered in the "product" text field
                var chkBox = document.createElement("input"); //creates a new input based on the user input
                chkBox.type = "checkbox"; //creates a checkbox next to the user input from "product"

                //form the relationship
                li.appendChild(chkBox); //
                li.appendChild(childTextNode); //
                //attach to the parent element
                list.appendChild(li); //
                inputText.value = "";
                inputText.focus();
            }
        </script>
    </head>

    <body>
        <div id="content">
            <h1>Shopping Cart</h1>
            <input id="product" type="text" />
            <input type="button" id="Add" value="Add" onclick="addIt()" />
            <input type="button" id="Delete" value="Delete" onclick="deleteIt()" />
            <ul id="list">
                <li>
                    <input type="checkbox" />Eggs</li>
            </ul>
        </div>
        </div>
    </body>

</html>
4

3 回答 3

0

您需要将它们作为父包装器的子级删除。

然后你可以使用这个: https ://developer.mozilla.org/En/DOM/Node.removeChild

在将子项传递给方法时将其删除

function deleteCheckbox(cbId){
  document.getElementById("content").removeChild(document.getElementById(cbId));
}

在你的 html 中

<checkbox onclick="deleteCheckbox(id)">

假设你知道它的 ID。如果你不这样做,你可以根据它的顺序访问它。我不完全清楚您希望删除哪个复选框,但它的完成方式与您添加孩子的方式相同,只是相反:)

于 2012-05-31T03:55:28.460 回答
0

通用removeElement函数可能是:

function removeElement(element) {
  if (element && element.parentNode) {
    element.parentNode.removeChild(element);
  }
}

// Use example
removeElement(document.getElementById('someId'));
于 2012-05-31T04:13:57.053 回答
0

在 html 中输入(添加名称标签)

  <ul id="list">
    <li><input type="checkbox" name='food'>Eggs</input></li>
  </ul>

添加时(在 addIt() 中将名称添加到复选框)

 chkBox.type="checkbox";                                 

 chkBox.name = 'food';

现在 deleteIt 函数

function deleteIt(){
    var food = document.getElementsByName('food'); // get the checkbox
    for (i=0; i<food.length; i++) // loop through it
    {
    // if the current state is checked, unchecked and vice-versa
            if (food[i].checked) // if its checked
            {
            var remElm = food[i];   // element to be removed
        var list=document.getElementById("list"); // also li to be removed           
        food[i].nextSibling.nodeValue = ''; // Text value set to 
        remElm.parentNode.removeChild(remElm);
        ul = document.getElementsByTagName('ul')[0];
        li = ul.getElementsByTagName('li');
        ul.removeChild(li[i]);
            } else {       
            }
    }
}
于 2012-05-31T04:50:49.777 回答