<!DOCTYPE html>
<html lang="en">
<head>
<script>
function calculate(){
var salary = document.forms["calculateForm"]["salary"].value;
document.getElementById("werd").getInnerHTML = salary;
return false;
}
</script>
<title> Question </title>
</head>
<body onload = "calculate()">
<form action = "" id = "calculateForm" onsubmit = "return calculate();">
Salary:
<input type = "text" name = "salary" value = "95000" style="background-color:lightgray" required>
<input type = "submit" value = "Submit">
</form>
<p id = "werd"> Text that should be replaced with the entered salary upon clicking Submit which runs a JavaScript function </p>
</body>
</html>
你好,
输入工资并按“提交”后,由于调用了calculate()函数,id="werd"的段落应更新为工资。为什么这没有发生?
我花了 3-4 个小时尝试解决这个问题,并在 google 上查找了很多解决方案。有什么解决方案(除了学习 jQuery 等)吗?
谢谢!
最佳答案
将其从 getInnerHTML
更改为 innerHTML
,一切正常。
function calculate() {
var salary = document.forms["calculateForm"]["salary"].value;
document.getElementById("werd").innerHTML = salary;
return false;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title> Question </title>
</head>
<body onload="calculate()">
<form action="" id="calculateForm" onsubmit="return calculate();">
Salary:
<input type="text" name="salary" value="95000" style="background-color:lightgray" required>
<input type="submit" value="Submit">
</form>
<p id="werd"> Text that should be replaced with the entered salary upon clicking Submit which runs a JavaScript function </p>
</body>
</html>
关于JavaScript - 输入提交按钮 (html) 以激活 JavaScript 功能不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43903533/