简体   繁体   English

如何修复我的清除按钮以删除数组

[英]How to fix my Clear button to remove array

I really need help with getting my button's onclick to clear.我真的需要帮助让我的按钮的 onclick 清除。 struggling to understand how I can clear the array names once they have been inputted.努力理解如何在输入数组名称后清除它们。 I've only recently been studying Javascript and my knowledge in Html is only mediocre.我最近才在学习 Javascript,而我在 Html 方面的知识也很一般。

 function Add() { //create a para obj var newPara = document.createElement("p"); //create the text content var input = document.getElementById("txtName").value; var newText = document.createTextNode(input); //attach the text to the para obj newPara.appendChild(newText); //attach the para obj to the div obj var myDiv = document.getElementById("result"); myDiv.appendChild(newPara); } function Clear() { var newPara = document.getElementById("tostring") input = "" newPara.value = 0; } function Count() { var arrPara = document.getElementById("result").childNodes; document.getElementById("result").removeChild(arrPara[Index.Count]); }
 <label>Name</label><input id="txtName" type="text" /><br /> <p id="displayNames"></p> <button onclick="Add()">Add</button> <!-- add the name to an array and display the name on the paragraph --> <button onclick="Clear()">Clear</button> <!-- clear the names in the array and clear the paragraph --> <button onclick="Count()">Count</button> <!-- count and display the number of times the name has been entered --> <div id="result"></div>

You can clear the value in side the add() . 您可以在add()旁边清除值。

Please Note: You do not have any element with #tostring in your HTML. 请注意:您的HTML中没有任何带有#tostring元素。

Change: 更改:

//create the text content
var input = document.getElementById("txtName").value;
var newText = document.createTextNode(input);

To: 至:

//create the text content
var input = document.getElementById("txtName");
var newText = document.createTextNode(input.value);
input.value = ''; // clear the value

it will clear the input value, when you clicked on the button.当您单击按钮时,它将清除输入值。

 function Add() { //create a para obj var newPara = document.createElement("p"); //create the text content var input = document.getElementById("txtName").value; var newText = document.createTextNode(input); //attach the text to the para obj newPara.appendChild(newText); //attach the para obj to the div obj var myDiv = document.getElementById("result"); myDiv.appendChild(newPara); document.getElementById('txtName').value = ''; } function Clear() { var newPara = document.getElementById("result") newPara.textContent = ''; console.log(newPara) newPara.value = 0; } function Count() { var arrPara = document.getElementById("result").childNodes; document.getElementById("result").removeChild(arrPara[Index.Count]); }
 <label>Name</label><input id="txtName" type="text" /><br /> <p id="displayNames"></p> <button onclick="Add()">Add</button> <!-- add the name to an array and display the name on the paragraph --> <button onclick="Clear()">Clear</button> <!-- clear the names in the array and clear the paragraph --> <button onclick="Count()">Count</button> <!-- count and display the number of times the name has been entered --> <div id="result"></div>

Thanks!谢谢!

function Clear() {
 document.getElementById("result").textContent ="";
 document.getElementById("txtName").value ="";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM