gpt4 book ai didi

javascript - 如何获取用户输入并输出值?

转载 作者:行者123 更新时间:2023-11-30 14:32:25 35 4
gpt4 key购买 nike

您好,我想获取用户写的内容,将其乘以某个数字并在标题中显示结果。

    <!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;
}

最佳答案

你有一些不必要的代码。你真的需要:

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>

关于javascript - 如何获取用户输入并输出值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50970578/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com