HTML按钮不会调用函数。怎么了?

时间:2016-08-26 07:14:39

标签: javascript function

我的html按钮无法调用函数。我不知道什么是错的。该功能似乎没问题,但是当我调用它时它什么也没做。如果这个问题很愚蠢或者我做错了什么,我很抱歉。



function max()
{
 var number1 = document.getElementById('first');
 var number2 =  document.getElementById('second');
    if (number1>number2)
    {
     document.getElementById('result').innerHTML = "The first number is larger";
    }
  else if (number1===number2)
    {
       document.getElementById('result').innerHTML = "The numbers are equal";
    }
  else
    {
      document.getElementById('result').innerHTML = "The second number is larger";
    }
  }

 <div id="box">
   <h3>Maximum between two numbers</h3>
   <p>First number: <input type="text"; id="first"></p><br>
  <p>Second number: <input type="text"; id="second"></p>
   <input type="submit"; id="submit"; value="Maximum"; onclick ="max()"> <br>
   <p>The result is:</p><span id="result"> </span>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

问题在于功能名称:

function max_demo()
       {
         
        var number1 = document.getElementById('first');
        var number2 =  document.getElementById('second');
           if (number1>number2)
           {
            document.getElementById('result').innerHTML = "The first number is larger";
           }
         else if (number1===number2)
           {
              document.getElementById('result').innerHTML = "The numbers are equal";
           }
         else
           {
             document.getElementById('result').innerHTML = "The second number is larger";
           }
         }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div id="box">
   <h3>Maximum between two numbers</h3>
   <p>First number: <input type="text"; id="first"></p><br>
  <p>Second number: <input type="text"; id="second"></p>
   <input type="submit" id="submit" value="Maximum" onclick ="max_demo()"> <br>
   <p>The result is:</p><span id="result"> </span>
</div>

答案 1 :(得分:-1)

max本身是input元素中的一个属性。直接从DOM元素调用时,有些名称不能用于函数。使用其他功能名称。

请参阅this回答。