繁体   English   中英

JavaScript - 输入提交按钮(html)以激活JavaScript功能不起作用

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM