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

标签 javascript

<!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/

相关文章:

javascript - javascript生成内容的类选择器

javascript - 在手机和平​​板电脑中使用 jQuery mobile 开发的网站的行为

javascript - 使用 javascript 进行十进制验证

javascript - 在 Dojo 的父元素内部创建元素

JavaScript 性能 : Multiple variables or one object?

javascript - 如何删除子项并获取父项

javascript - 通过 Input React 将参数从子级传递给父级

javascript - 时刻.js : Get first day of first week by given year

javascript - 使用 JavaScript 打开动态链接新窗口(但不是弹出窗口)

javascript - 是否可以将 html 转换为 Node 类型?