简体   繁体   中英

JavaScript function won't fire on button click

I've just started teaching myself JavaScript and am doing some basic exercises. 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).

chrome developer tool says it has syntax error!

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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