简体   繁体   English

我在此javascript计算中做错了什么?

[英]What have i done wrong in this javascript calculation?

I have two inputs that can hold a value and where a value is typed in. 我有两个输入可以容纳一个值,并在其中输入值。
calculate() kinda do the job but I getting an error in the console : calculate()完成这项工作,但在控制台中出现错误:
Uncaught TypeError: Cannot set property 'value' of null(…)
But for calculate() I get only that error! 但是对于calculate()我只会得到该错误!

    if($sal>10){
         echo '<td>
         <input type="text" oninput="calculate()"  name="monthly_sum_019" id="monthly_sum_019" value="">
         <input type="hidden" class="input toggle all" id="monthly_sum_019" oninput="calculate()" value="" placeholder="'.$sal.'"></td>';       
    }else{
         echo '<td>
         <input type="text" oninput="calculate()"  name="monthly_sum_019" id=""  value="">
         <input type="hidden" class="input toggle all" id="monthly_sum_019" oninput="calculatez()"  value="'.$sal.'"></td>';
        }

And have 2 inputs that returns result of calculation: 并有2个输入返回计算结果:

if($sal>10){
     echo '<td>
     <input type="text" id="result" name="reward_019"  value=""></td>'; 
}else{
     echo '<td>
     <input type="text" id="result2"  name="reward_019"  value=""></td>';           
    }

And the javascript: 和javascript:

<script>
    function calculate() {
        var myBox1 = parseFloat(document.getElementById('monthly_sum_019').value);  
        var myBox2 = parseFloat(document.getElementById('norm').value);
        var myBox3 = parseFloat(document.getElementById('time_hours_019').value);
        var result = document.getElementById('result'); 
        var myResult = (myBox1 / myBox2) * myBox3;

    result.value = myResult.toFixed(2);
    }   
</script>
<script>
    function calculatez() {
        var myBox1 = parseFloat(document.getElementById('hourmoney').value);    
        var myBox3 = parseFloat(document.getElementById('time_hours_019').value);
        var result2 = document.getElementById('result2');   
        var myResult = myBox3 * myBox1;
            result2.value = myResult.toFixed(2);                
    }
</script>

What is wrong? 怎么了?

Usually Cant do something of NULL means you try to select element but it's not presented in DOM. 通常, Cant do something of NULL意味着您尝试选择元素,但该元素未在DOM中显示。

I see that you try to select element with ID result2 but in your provided code I see that when $sal > 10 == true you do not put element with such ID. 我看到您尝试选择ID为result2元素,但是在您提供的代码中,我看到当$sal > 10 == true您不会放置具有该ID的元素。

Since it's PHP code you can have same ID in both if and else parts (but not in same part multiple times). 由于它是PHP代码,因此您可以ifelse部分中使用相同的ID(但不能在同一部分中多次使用)。

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

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