javascript按钮onclick不会触发

时间:2012-10-22 19:44:44

标签: javascript html5 button onclick

所以我做了这个简单的任务,但我不明白为什么这不起作用?同一个人可以看到并告诉我吗?我看到问题是按钮没有调用函数,但为什么?

<html>
<head>
    <title>Task count</title>
    <script>
        var over21 = 6.19;
        var under21 = 5.19;

        // Getting data from a user connecting variables with textboxes by ID
        var age = document.getElementById('age');
        var hours = document.getElementById('hours');

        // function wich does all of the calculation
        function magic(){    
            //cheking if he is under or over 21 
            if(age < 21){
                alert("You will make " + (age*under21));
            }else{
                alert("You will make " + (age*over21));                            
            };
        };

        // function that does everything and is connected to the button
        function myFunction(){
            // Validation checking that entered number is not more than 150 
            // And also if age is correct
            if((hours < 150 && hours > 0) && (age < 150 && age > 12)){
                magic();
            }else{
                alert("Wrong!");
            };
        };
  </script>
</head>
<body>
    <label>Enter your age.(12-150)</label>
    <input type="text" class="textBox" id="age"><br>
    <label>Enter how long will you work.(1-150)</label>
    <input type="text" class="textBox" id="hours"><br>
    <button onclick="myFunction()" >How much will i make ?</button>
</body>
</html>​

5 个答案:

答案 0 :(得分:4)

var age = document.getElementById('age').value;
var hours = document.getElementById('hours').value;

你得到的HTML元素对象不是它们包含的“值”。

答案 1 :(得分:1)

我建议不要使用这么多全局变量。

移动:

var age = document.getElementById('age');
var hours = document.getElementById('hours');

进入myFunction()

然后你需要制作这两个:

var age = parseInt(document.getElementById('age').value);
var hours = parseInt(document.getElementById('hours').value);

我们使用parseInt将值作为字符串并将其转换为整数值。

由于现在在age中定义了hoursmyFunction,您需要将age传递给magic()

    var over21 = 6.19;
    var under21 = 5.19;

    // function wich does all of the calculation
    function magic(age){

      //cheking if he is under or over 21 
       if(age < 21){
             alert("You will make " + (age*under21));
       }else{
             alert("You will make " + (age*over21));                            
       };
    };
    // function that does everything and is connected to the button
    function myFunction(){

    var age = parseInt(document.getElementById('age').value);
    var hours = parseInt(document.getElementById('hours').value);
     // Validation checking that entered number is not more than 150 // And also if age is correct
     console.log(age + " : " + hours)   
        if((hours < 150 && hours > 0) && (age < 150 && age > 12)){
           magic(age);
        }else{
            alert("Wrong!");
        };
    };

,如果您需要(1-150),则需要修改此if声明:

if((hours < 150 && hours > 0) && (age < 150 && age > 12)){ ... }

为:

if((hours <= 150 && hours > 0) && (age <= 150 && age > 12)){ ... }

最后,我认为magic()函数中的数学运算可能不正确:

function magic(age, hours){

  //cheking if he is under or over 21 
   if(age < 21){
         alert("You will make " + (hours*under21));
   }else{
         alert("You will make " + (hours*over21));                            
   };
};

我相信你想要hours*under21hours*over21。请注意,hours现在也作为参数传入。

<强> EXAMPLE

答案 2 :(得分:0)

在您尝试获取的元素存在之前,getElementById调用正在运行。

您可以在函数内移动它们,也可以移动到<script>末尾的另一个<body>

然后,要访问该值本身,请使用age.valuehours.value。您还应该通过parseInt(...,10)运行它们以确保它们是数字。

答案 3 :(得分:0)

如果这是我可以发现3个错误的代码:

  1. 你在加载之前访问dom只需要一次,尝试在你需要的时候分配vars。
  2. 你要为vars分配dom对象而不是它们的值,使用getElementById(“mycoolid”)。value
  3. 您没有将类型分配给按钮元素,不同的浏览器会为此属性分配不同的默认值。

答案 4 :(得分:0)

设置年龄和小时变量的代码只运行一次,因此它们是这些文本框的空/默认值。您应该在函数范围之外声明它们,但是然后在“myFunction”函数内重新设置它们。或者,您只能在“myFunction”函数中声明/设置它们,然后将它们作为参数传递给“magic”函数。最后,您的脚本应该看起来像这样:

<html>
<head>
    <title>Task count</title>
    <script>
        var over21 = 6.19;
        var under21 = 5.19;

        // function wich does all of the calculation
        function magic(age, hours){    
            //cheking if he is under or over 21 
            if(age < 21){
                alert("You will make " + (age*under21));
            }else{
                alert("You will make " + (age*over21));                            
            };
        };

        // function that does everything and is connected to the button
        function myFunction(){
            // Getting data from a user connecting variables with textboxes by ID
            var age = document.getElementById('age').value;
            var hours = document.getElementById('hours').value;

            // Validation checking that entered number is not more than 150 
            // And also if age is correct
            if((hours < 150 && hours > 0) && (age < 150 && age > 12)){
                magic(age, hours);
            }else{
                alert("Wrong!");
            };
        };
  </script>
</head>
<body>
    <label>Enter your age.(12-150)</label>
    <input type="text" class="textBox" id="age"><br>
    <label>Enter how long will you work.(1-150)</label>
    <input type="text" class="textBox" id="hours"><br>
    <button onclick="myFunction()" >How much will i make ?</button>
</body>
</html>​

作为补充说明,您将年龄乘以21或21以上。我很确定你想要将小时乘以21或21以上。