嗨,我想获得用户写的内容,将其乘以某个数字并在标题中显示结果。
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<input type="text" id="number" name="num" value="">
<div id = "output">
<h2 id="OP"></h2>
</div>
</body>
</html>
var valResult = parseInt(document.getElementById("number").value);
var num = 5;
var results = valResult + num;
if (valResult == 10){
document.getElementById("OP").innerHTML = results;
}
答案 0 :(得分:2)
您有一些不必要的代码。您真的需要:
if (valResult == 10){....
尝试以下操作:
function calculate(input){
var valResult = parseInt(input.value);
var num = 5;
var results = valResult * num;
document.getElementById("OP").innerHTML = results;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<input type="text" id="number" name="num" oninput="calculate(this)">
<div id = "output">
<h2 id="OP"></h2>
</div>
</body>
</html>