2 fenglinstudio fenglinstudio 于 2016.04.24 18:44 提问

问题又来了,分别赋值了9个之后为什么跑起来只有一个能运算?不清楚问题出在哪里了

图片说明

 <html> 
<head>
<script type='text/javascript'>
onload=compute;
function compute(){
    a=parseFloat(document.getElementById("txt1").value);
    b=parseFloat(document.getElementById("txt2").value);
    if(a!="" && b!=""){
        document.getElementById("txt3").value=(a/b).toFixed(2);
    }
}
</script>







<script type='text/javascript'>
onload=compute;
function compute(){
    c=parseFloat(document.getElementById("txt4").value);
    d=parseFloat(document.getElementById("txt5").value);
    if(c!="" && d!=""){
        document.getElementById("txt6").value=(c/d).toFixed(2);
    }
}
</script>
<script type='text/javascript'>
onload=compute;
function compute(){
    e=parseFloat(document.getElementById("txt7").value);
    f=parseFloat(document.getElementById("txt8").value);
    if(e!="" && f!=""){
        document.getElementById("txt9").value=(e/f).toFixed(2);
    }
}
</script>
<script type='text/javascript'>
onload=compute;
function compute(){
    g=parseFloat(document.getElementById("txt10").value);
    h=parseFloat(document.getElementById("txt11").value);
    if(g!="" && h!=""){
        document.getElementById("txt12").value=(g/h).toFixed(2);
    }
}
</script>
<script type='text/javascript'>
onload=compute;
function compute(){
    i=parseFloat(document.getElementById("txt13").value);
    j=parseFloat(document.getElementById("txt14").value);
    if(a!="" && b!=""){
        document.getElementById("txt15").value=(i/j).toFixed(2);
    }
}
</script>
<script type='text/javascript'>
onload=compute;
function compute(){
    k=parseFloat(document.getElementById("txt16").value);
    l=parseFloat(document.getElementById("txt17").value);
    if(k!="" && l!=""){
        document.getElementById("txt18").value=(k/l).toFixed(2);
    }
}
</script>
<script type='text/javascript'>
onload=compute;
function compute(){
    m=parseFloat(document.getElementById("txt19").value);
    n=parseFloat(document.getElementById("txt20").value);
    if(m!="" && n!=""){
        document.getElementById("txt21").value=(m/n).toFixed(2);
    }
}
</script>
<script type='text/javascript'>
onload=compute;
function compute(){
    o=parseFloat(document.getElementById("txt22").value);
    p=parseFloat(document.getElementById("txt23").value);
    if(o!="" && p!=""){
        document.getElementById("txt24").value=(o/p).toFixed(2);
    }
}
</script>
<script type='text/javascript'>
onload=compute;
function compute(){
    q=parseFloat(document.getElementById("txt25").value);
    r=parseFloat(document.getElementById("txt26").value);
    if(q!="" && r!=""){
        document.getElementById("txt27").value=(q/r).toFixed(2);
    }
}
</script>
</head> 
<body>
<input  id="txt1"  type="hidden" value="123123" size="1" readonly oninput="compute()"> 
<input  id="txt2"  type="hidden" value="123123" size="1"readonly oninput="compute()"> 
<input id="txt3" size="1" readonly>
<br>







<input  id="txt4"  type="hidden" value="123123" size="1" readonly oninput="compute()"> 
<input  id="txt5"  type="hidden" value="123123" size="1"readonly oninput="compute()"> 
<input id="txt6" size="1" readonly>
<br>






<input  id="txt7"  type="hidden" value="123123" size="1" readonly oninput="compute()"> 
<input  id="txt8"  type="hidden" value="123123" size="1"readonly oninput="compute()"> 
<input id="txt9" size="1" readonly>
<br>




<input  id="txt10"  type="hidden" value="123123" size="1" readonly oninput="compute()"> 
<input  id="txt11"  type="hidden" value="123123" size="1"readonly oninput="compute()"> 
<input id="txt12" size="1" readonly>
<br>





<input  id="txt13"  type="hidden" value="123123" size="1" readonly oninput="compute()"> 
<input  id="txt14"  type="hidden" value="123123" size="1"readonly oninput="compute()"> 
<input id="txt15" size="1" readonly>
<br>





<input  id="txt16"  type="hidden" value="123123" size="1" readonly oninput="compute()"> 
<input  id="txt17"  type="hidden" value="123123" size="1"readonly oninput="compute()"> 
<input id="txt18" size="1" readonly>
<br>





<input  id="txt19"  type="hidden" value="123123" size="1" readonly oninput="compute()"> 
<input  id="txt20"  type="hidden" value="123123" size="1"readonly oninput="compute()"> 
<input id="txt21" size="1" readonly>
<br>





<input  id="txt22"  type="hidden" value="123123" size="1" readonly oninput="compute()"> 
<input  id="txt23"  type="hidden" value="123123" size="1"readonly oninput="compute()"> 
<input id="txt24" size="1" readonly>
<br>





<input  id="txt25"  type="hidden" value="123123" size="1" readonly oninput="compute()"> 
<input  id="txt26"  type="hidden" value="123123" size="1"readonly oninput="compute()"> 
<input id="txt27" size="1" readonly>










</body> 
</html> 

1个回答

showbo
showbo   Ds   Rxr 2016.04.24 20:56
已采纳

同名函数会被覆盖,而且xxxx=赋值事件也会覆盖,写一个函数就行了,传入参数

 <html>
<head>
    <script type='text/javascript'>
        window.onload = function () {
            for (var i = 1; i <= 27; i += 3) compute('txt' + i, 'txt' + (i + 1), 'txt' + (i + 2))
        };
        function compute(a,b,c) {
            q = parseFloat(document.getElementById(a).value);
            r = parseFloat(document.getElementById(b).value);
            if (q && r) {
                document.getElementById(c).value = (q / r).toFixed(2);
            }
        }
    </script>
</head>
<body>
    <input id="txt1" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt2" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt3" size="1" readonly>
    <br>







    <input id="txt4" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt5" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt6" size="1" readonly>
    <br>






    <input id="txt7" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt8" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt9" size="1" readonly>
    <br>




    <input id="txt10" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt11" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt12" size="1" readonly>
    <br>





    <input id="txt13" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt14" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt15" size="1" readonly>
    <br>





    <input id="txt16" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt17" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt18" size="1" readonly>
    <br>





    <input id="txt19" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt20" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt21" size="1" readonly>
    <br>





    <input id="txt22" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt23" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt24" size="1" readonly>
    <br>





    <input id="txt25" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt26" type="hidden" value="123123" size="1" readonly oninput="compute()">
    <input id="txt27" size="1" readonly>










</body>
</html> 
Csdn user default icon
上传中...
上传图片
插入图片