[英]Adding a clear button with HTML and JavaScript
I can't figure out why this won't work!我不明白为什么这行不通! My clear button does nothing .我的清除按钮什么也不做。 Here is my entire form/calculator.这是我的整个表格/计算器。 I need the "clear" button just to get rid of the text in the "feet" and "meters" fields.我需要“清除”按钮来清除“英尺”和“米”字段中的文本。
I'm working on a class project.我正在做一个课堂项目。
<body>
<form name="myForm">
<table border = "0" width = 300px>
<tr>
<td>
Feet:
</td>
<td>
<input type="text" id="feet">
</td>
</tr>
<tr>
<td>
Meters:
</td>
<td>
<input type="text" id="meters">
</tr>
</table>
</form>
</body>
<p>
<button onClick="calculate()">Convert</button>
<button onClick="clear()">Clear</button>
<p>
<script>
function calculate()
{
var feet = document.getElementById("feet").value;
var meters = document.getElementById("meters").value;
var final;
if (feet == "" && meters == "")
{
alert("Try Again");
return;
}
else if (!(feet == "") && !(meters == ""))
{
alert("Try Again");
return;
}
else if (feet == "")
{
final = (meters*(3.28084));
//Display!
final = final.toFixed(2);
document.getElementById("feet").value = final;
}
else if (meters == "")
{
final = (feet*(.3048));
//Display!
final = final.toFixed(2);
document.getElementById("meters").value = final;
}
}
//****************************************************************
function clear()
{
document.getElementById("myForm").reset();
}
</script>
The actual problem is that you are using this实际的问题是你正在使用这个
document.getElementById("myForm").reset();
but your form has no ID, so you would add the ID但是您的表单没有 ID,因此您需要添加 ID
<form name="myForm" id="myForm">
That will work for you, but if you want to fix your code read on.这对你有用,但如果你想修复你的代码,请继续阅读。
You have a few errors.你有几个错误。 Your form needs an id because you're using getElementById
您的表单需要一个 id,因为您使用的是getElementById
There's no need to use a button to submit javascript that does what a button can do.无需使用按钮来提交执行按钮功能的 javascript。
You have </body>
then more HTML.你有</body>
然后更多的 HTML。
There's a missing </td>
缺少一个</td>
<table border = "0" width = 300px>
is not correct syntax (and you should really use the style attribute or a CSS file) <table border = "0" width = 300px>
语法不正确(你真的应该使用 style 属性或 CSS 文件)
Also you can use a submit button and bind the function to the action of the form being submitted, which I've done here.您也可以使用提交按钮并将函数绑定到正在提交的表单的操作上,我已经在这里完成了。
<body>
<form name="myForm" id="myForm">
<table border = "0" width = "300">
<tr>
<td>Feet:</td>
<td><input type="text" id="feet"></td></tr>
<tr>
<td>Meters:</td>
<td><input type="text" id="meters"></td>
</tr>
</table>
<button type="submit">Convert</button>
<button type="reset">Clear</button>
</form>
<script>
document.getElementById("myForm").onsubmit=function(){
calculate();
return false;
};
function calculate()
{
var feet = document.getElementById("feet").value;
var meters = document.getElementById("meters").value;
var final;
if (feet == "" && meters == "")
{
alert("Try Again");
return;
}
else if (!(feet == "") && !(meters == ""))
{
alert("Try Again");
return;
}
else if (feet == "")
{
final = (meters*(3.28084));
//Display!
final = final.toFixed(2);
document.getElementById("feet").value = final;
}
else if (meters == "")
{
final = (feet*(.3048));
//Display!
final = final.toFixed(2);
document.getElementById("meters").value = final;
}
}
</script>
</body>
<button type="reset" value="Reset">
Why do you have to use JS to accomplish this outside of the JS attached to the clear "button"?为什么必须在附加到清晰“按钮”的 JS 之外使用 JS 来完成此操作? I would stick to HTML and just use a simple form reset.我会坚持使用 HTML,只使用简单的表单重置。
Or, try this here: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_form_reset或者,在这里试试这个: http : //www.w3schools.com/jsref/tryit.asp?filename=tryjsref_form_reset
将<form name="myForm">
更改为<form name="myForm" id="myForm">
因为您正在寻找具有 id myForm
的元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.