简体   繁体   English

单击按钮时不会触发JavaScript函数

[英]JavaScript function won't fire on button click

I've just started teaching myself JavaScript and am doing some basic exercises. 我刚刚开始自学JavaScript,正在做一些基本的练习。 Right now I'm trying to make a function that takes in three values and prints out the largest. 现在我正在尝试创建一个函数,它接受三个值并打印出最大值。 However, the function refuses to fire upon button click. 但是,该功能在按钮点击时拒绝触发。 I tried making a separate, very simple function just for debugging and it refuses to fire as well. 我尝试制作一个单独的,非常简单的函数,仅用于调试,它也拒绝触发。 I've written other practice functions in almost the exact same manner (they had two parameters as opposed to three) that work fine. 我已经用几乎完全相同的方式编写了其他练习函数(它们有两个参数,而不是三个参数),工作正常。 Any insight would be greatly appreciated. 任何见解将不胜感激。

<!DOCTYPE HTML>
<html>
<head>
<script type = "text/javascript">
<!--

//Define a function Max() that takes three numbers as 
//arguments and returns the largest of them.
function Boo(){
    document.write("boo");
    alert("boo");
}


function Max(p1, p2, p3){
    document.write(p1+p2+p3);
    alert('BOO!')

    document.write(document.getElementById('value1').value);

    var max = Number(p1);
    v2 = Number(p2):
    v3 = Number(p3);    

    if (v2 > max)
        max = v2;
    if (v3 > max)
        max = v3;

    document.write(max);
}
-->
</script>
    </head>
    <body>
<form>
    <input type="text" id="value1" name="value1"/><br />
    <input type="text" id="value2" name="value2"/><br />
    <input type="text" id="value3" name="value3"/><br />
    <input type = "button" 
    onclick="Boo()" 
    value = "Compare!"/>
    <!-- onclick="Max(document.getElementById('value1').value,
                 document.getElementById('value2').value,
                 document.getElementById('value3').value)" -->
</form>
  </body>
 </html>
  v2 = Number(p2):

should be 应该

v2 = Number(p2);

Also, look into the developer tools for whatever browser you are using to find simple syntax errors like this. 另外,请查看开发人员工具,了解您使用的任何浏览器,以找到这样的简单语法错误。 (In most browsers you can press F12). (在大多数浏览器中,您可以按F12键)。

chrome developer tool says it has syntax error! chrome开发人员工具说它有语法错误!

v2 = Number(p2):

should be: 应该:

v2 = Number(p2);

您的javascript函数已用<!-- -->注释掉

This is all you need: 这就是你所需要的:

    var elements = document.getElementsByName('to_compare');
    var elementsAsArray = [].slice.call(elements);
    var numberValues = elementsAsArray.map(function(x) {
        return Number(x.value)
    });
    return Math.max.apply(this, numberValues);

Full demo, including using console.log rather than document.write , also including how to append to document without clearing it: http://jsfiddle.net/yYhjD/1/ 完整演示,包括使用console.log而不是document.write ,还包括如何在不清除它的情况下附加到文档: http//jsfiddle.net/yYhjD/1/

<head>
    <script type="text/javascript">

function compare() {
    var elements = [].slice.call(document.getElementsByName('to_compare'));
    var numberValues = elements.map(function(x){return Number(x.value)});
    return Math.max.apply(this, numberValues);
}

    </script>
</head>

<body>
    <input type="text" id="value1" name="to_compare" value="-10"/>
    <input type="text" id="value2" name="to_compare" value="080"/>
    <input type="text" id="value3" name="to_compare" value="79"/>
    <br/>
    <input type="button" onclick="console.log(compare())" value="Compare!"/>
</body>

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

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