简体   繁体   English

如何清除表格中的信息?

[英]How do i clear information in a form?

I am doing my first javascript app (some kind of calculator, where you write 2 numbers and you can add them, substract them, multiply them and divide them) i want to make a button that lets you clear the numbers you typed, i have searched on the web but i can't find exactly how to do it;我正在做我的第一个 javascript 应用程序(某种计算器,你可以在其中写 2 个数字,然后你可以将它们相加、相减、相乘和相除)我想做一个按钮,让你清除输入的数字,我有在 web 上搜索,但我找不到确切的方法; my code is this:我的代码是这样的:

 function addBy() { num1 = document.getElementById("firstNumber").value; num2 = document.getElementById("secondNumber").value; document.getElementById("result").innerHTML = +num1 + +num2; } function substractBy() { num1 = document.getElementById("firstNumber").value; num2 = document.getElementById("secondNumber").value; document.getElementById("result").innerHTML = +num1 - +num2; } function multiplyBy() { num1 = document.getElementById("firstNumber").value; num2 = document.getElementById("secondNumber").value; document.getElementById("result").innerHTML = num1 * num2; } function divideBy() { num1 = document.getElementById("firstNumber").value; num2 = document.getElementById("secondNumber").value; document.getElementById("result").innerHTML = num1 / num2; } function clear() { document.getElementById("result").innerHTML = ""; }
 <form> 1st Number: <input type="number" id="firstNumber" /><br> 2nd Number: <input type="number" id="secondNumber" /><br> <input type="button" onClick="addBy()" value="Add" /><br> <input type="button" onClick="substractBy()" value="Substract" /><br> <input type="button" onClick="multiplyBy()" Value="Multiply" /><br> <input type="button" onClick="divideBy()" Value="Divide" /><br> <input type="button" onClick="clear()" value="Clear" /><br> </form>

can anyone help?谁能帮忙?

Because it seems like your aim is to have calculator-like functionality here, your clear() function just needs to clear the inputs on the page, as well as the result.因为您的目标似乎是在这里拥有类似计算器的功能,所以您的clear() function 只需要清除页面上的输入以及结果。

Similar to how you are getting the values for firstNumber and secondNumber , you can also set those values to an empty string.与获取firstNumbersecondNumber值的方式类似,您也可以将这些值设置为空字符串。

 function clear() { document.querySelector("#firstNumber").value = ""; document.querySelector("#secondNumber").value = ""; document.querySelector("#result").innerHTML = ""; }

If you want to clear only inputs如果您只想清除输入

Input Type Reset can do that, Just add输入类型重置可以做到这一点,只需添加

<input type="reset" value="Clear"/>

Or或者

<button type="reset">Clear</button>

At your form and then whenever anyone clicks it, it will clear all the inputs taken from the user.在您的表单中,然后每当有人单击它时,它将清除从用户那里获取的所有输入。

JavaScript Approach as a Function JavaScript 方法作为 Function

function clear() {
   document.getElementById('#form-id').reset();
}

If you want to clear inputs and the result如果要清除输入和结果

Javascript Function Javascript Function

function clear() { 
   document.getElementById('#firstNumber').value = ''; 
   document.getElementById('#secondNumber').value = ''; 
   document.getElementById('#result').innerHTML = '';
}

I hope it will work.我希望它会奏效。

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

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