按钮单击“未定义”不是一个函数

button onclick Undefined is not a function

本文关键字:未定义 一个 函数 单击 按钮      更新时间:2023-09-26

我觉得这应该是世界上最容易的事情。我只想用点击调用制作一个按钮,并让它运行 javascript。然而,每次我这样做时,我都会得到未捕获的类型错误:未定义不是函数错误

我已经阅读了这个,但似乎不明白为什么它找不到该功能。

回顾一下:点击按钮应该运行 ThePush(),但我在控制台上收到错误。

法典:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
        function ThePush() {
          var name = document.getElementByID('name').value;
          var pl = document.getElementByID('PL1').value;
          var plw = document.getElementByID('theplw').value;
          var al = document.getElementByID('theal').value;
          //do stuff with these variables.
        }
</script>
</head>
<body>
<b>Player One: </b><input id="name" type="text" value="Name"></input>
<br/><b>Level: </b><input id="PL1" style="width:50px" type="text" id="PL1L" value="0" readonly></input>
<br/>
<button id="thesub" onclick="ThePush();">Submit</button>
<select id="theplw">
<option value="A: 0-999">A: 0-999</option>
<option value="B: 1,000-9,999">B: 1,000-9,999</option>
<option value="C: 10,000-99,999">C: 10,000-99,999</option>
<option value="D: 100,000-499,999">D: 100,000-499,999</option>
<option value="E: 500,000-1,499,999">E: 500,000-1,499,999</option>
<option value="F: 1,500,000+">F: 1,500,000+</option></select>
<br/><b>Sub Level: </b><input id="theal" style="width:50px" type="text" id="AL1L" value="0" readonly></input>
<hr>
<iframe src="others.php" frameborder="0" width="500"></iframe>
<iframe id="updater" src="update.php" frameborder="0" width="0" height="0"></iframe>
</body>
</html>

有什么想法吗?提前感谢您的帮助!

您应该将方法名称更改为 getElementById ,而不是 getElementByID

您的 onclick 很好,但 getElementByID 不是一个函数,并且会抛出您的错误。您正在寻找getElementById

    function ThePush() {
      var name = document.getElementById('name').value;
      var pl = document.getElementById('PL1').value;
      var plw = document.getElementById('theplw').value;
      var al = document.getElementById('theal').value;
      //do stuff with these variables.
    }

我只是偶然发现了同样的错误。我真的无法解释为什么(还不想),但是当我从更新程序更改按钮 ID 时,它开始工作。也许id和事件函数的相同名称导致了错误