【发布时间】:2020-01-18 09:37:28
【问题描述】:
所以,正如您在我的代码中看到的那样,无论我点击什么按钮,它都会输出到控制台。我希望它显示在输入类型 =“文本字段”中,我可能错过了一些可以相互计算两个数字的逻辑。请记住,我是 Javascript 新手,这是我的第一个项目。
我需要在文本字段中显示总和。
const buttons = document.querySelectorAll("input[type=button]");
const length = buttons.length;
for (let i = 0; i < length; i++) {
buttons[i].addEventListener("click", handle);
}
function handle(event) {
const value = event.target.value;
switch (value) {
case "+":
console.log("+ was clicked");
break;
case "-":
console.log("- was clicked");
break;
case "*":
console.log("+ was clicked");
break;
case "/":
console.log("/ was clicked");
break;
default:
//console.log("%s was clicked", value);
var myInput = document.getElementById("equal");
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Dizzy Calculator</title>
</head>
<body bgcolor="#b3b3cc" text="white">
<form name="calculator">
<h1>Calculator</h1>
<div class="num">
<tr>
<td>
<input type="button" value="1">
<input type="button" value="2">
<input type="button" value="3">
<input type="button" value="+">
</td>
</tr>
<br>
<tr>
<td>
<input type="button" value="4">
<input type="button" value="5">
<input type="button" value="6">
<input type="button" value="-">
</td>
</tr>
<br>
<tr>
<td>
<input type="button" value="7">
<input type="button" value="8">
<input type="button" value="9">
<input type="button" value="*">
</td>
</tr>
<br>
<tr>
<td>
<input type="button" value="/">
<input type="button" value="0">
<input type="reset" value="Reset">
</td>
</tr>
<br>
<input type="button" value="=" id="equal" onClick="document.calculator.ans.value=eval(document.calculator.ans.value)">
<br>Solution is <input type="textfield" name="ans" value="">
</div>
</form>
</body>
</html>
【问题讨论】:
-
你尝试了什么?
-
首先,检查w3schools.com/tags/att_input_type.asp 的输入类型。 textfield 是 text 之后,设置和输入字段,检查这里:stackoverflow.com/questions/5700471/…
标签: javascript html calculator