简体   繁体   English

按钮onclick未定义不是函数

[英]button onclick Undefined is not a function

I feel like this should be the easiest thing in the world. 我觉得这应该是世界上最简单的事情。 I just want to make a button with an onclick call and have it run javascript. 我只想制作一个带有onclick调用的按钮,并使其运行javascript。 Yet every time I do that I get the Uncaught TypeError: undefined is not a function error. 但是,每次这样做,我都会得到Uncaught TypeError:undefined不是函数错误。

I've read up on this but don't seem to understand why it can't find the function. 我已经阅读了此书,但似乎不明白为什么它找不到该函数。

To recap: Hitting the button should run ThePush() but I get an error on the console. 回顾一下:点击按钮应运行ThePush(),但在控制台上出现错误。

Code: 码:

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

Any thoughts? 有什么想法吗? Thank you in advance for your help! 预先感谢您的帮助!

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

Your onclick is fine, but getElementByID is not a function and is throwing your error. 您的onclick很好,但是getElementByID不是函数,并且引发您的错误。 You are looking for 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.
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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