[英]JavaScript - Input Submit Button (html) to activate JavaScript Function Not Working
<!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個小時試圖解決這個問題,並在谷歌上查找了很多解決方案。 任何解決方案(除了學習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>
document.getElementById("werd").getInnerHTML = salary;
改成
document.getElementById("werd").innerHTML = salary;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.