1

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

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

5 回答 5

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

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

于 2012-10-22T19:46:52.030 回答
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将值作为字符串并将其转换为整数值。

由于agehours现在定义在 中myFunction,您需要传递agemagic()

    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现在也作为参数传入。

例子

于 2012-10-22T19:55:50.743 回答
0

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

您可以将它们移动到函数内部,也可以将它们移动<script><body>.

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

于 2012-10-22T19:47:37.827 回答
0

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

  1. 您在加载 dom 之前访问它,并且只有一次,请尝试在需要时分配 var。
  2. 您正在为 var 分配 dom 对象而不是它们的值,请使用 getElementById("mycoolid").value
  3. 您没有将类型分配给按钮元素,不同的浏览器为此属性分配不同的默认值。
于 2012-10-22T19:58:46.677 回答
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 岁以上。我很确定您想将小时数乘以 under21 或 over21。

于 2012-10-22T20:02:12.417 回答