【问题标题】:can not add two multiplications result in text box in html无法在html中的文本框中添加两个乘法结果
【发布时间】:2014-09-16 17:34:26
【问题描述】:

先生 我有 7 个文本框。第 1 个从用户获取值并在单击时在第 3 个文本框上显示乘法结果。第 4 和第 5 个文本框也采用值并在单击时在第 6 个文本框中显示乘法结果。现在将显示第 7 个文本框单击时的加法结果。第 7 个文本框采用第 3 和第 6 个文本框的值。 我的问题是我无法添加,无法在 html 的第 7 个文本框中显示结果,jscript.plz 帮助我.....我附上了代码......

 <html>
  <script>
   function getText1(){
      var in1=document.getElementById('in1').value;
      var in2=document.getElementById('in2').value;


      var tot1=parseInt(in1)*parseInt(in2); 


      document.getElementById('tot1').value=tot1;
   }


   function getText2(){


      var in1=document.getElementById('in3').value;
      var in2=document.getElementById('in4').value; 

      var tot2=parseInt(in1)*parseInt(in2);           

      document.getElementById('tot2').value=tot2;
   }

   function gt1(){

   var in1=document.getElementById('tot1').value; 
   var in2=document.getElementById('tot2').value;

   var gt1=parseInt(in1)+parseInt(in2);
   document.getElementById('gt').value = gt1;



  </script>

 <form>
 <input type="text"id="in1"/>
 <input type="text" id="in2"/>
 <input type="text" onclick="getText1()" id="tot1"/>
 <br>
 <input type="text"id="in3"/>
 <input type="text" id="in4"/>
 <input type="text" onclick="getText2()" id="tot2"/>
  <br>
 <input type="text" onclick="gt1()" id="gt1"/>

</form>
</html>

【问题讨论】:

  • 必须是document.getElementById('gt1').value=gt1;(gt1 而不是gt)。
  • bt 仍然无法工作...请帮助
  • 有人请帮帮我....
  • 替换此代码 document.getElementById('gt1').value = gt1;

标签: javascript jquery html


【解决方案1】:
<html>
  <script>
   function getText1(){
      var in1=document.getElementById('in1').value;
      var in2=document.getElementById('in2').value;


      var tot1=parseInt(in1)*parseInt(in2); 


      document.getElementById('tot1').value=tot1;
   }


   function getText2(){


      var in1=document.getElementById('in3').value;
      var in2=document.getElementById('in4').value; 

      var tot2=parseInt(in1)*parseInt(in2);           

      document.getElementById('tot2').value=tot2;
   }

   function gt(){

   var in1=document.getElementById('tot1').value; 
   var in2=document.getElementById('tot2').value;

   var gt1=parseInt(in1)+parseInt(in2);
   document.getElementById('gt1').value = gt1;
}


  </script>

 <form>
 <input type="text"id="in1"/>
 <input type="text" id="in2"/>
 <input type="text" onclick="getText1()" id="tot1"/>
 <br>
 <input type="text"id="in3"/>
 <input type="text" id="in4"/>
 <input type="text" onclick="getText2()" id="tot2"/>
  <br>
 <input type="text" onclick="gt()" id="gt1"/>

</form>
</html>

使用它正在工作的代码。并与自己的代码进行比较。谢谢。。

【讨论】:

    【解决方案2】:

    有几个问题

    1)。元素id 是一行不正确。应该是:

    document.getElementById('gt1').value = gt1;
    

    2)。 result 变量未定义。应该是:

    if (!isNaN(gt1))
    

    3)。 onclick="gt1()" 不起作用(因为有同名的 id gt1)。使用,例如:

    function getText3() { ... }
    

    和

    <input type="text" onclick="getText3()" id="gt1"/>
    

    Fiddle.

    更新。好的,我没有注意到你想要乘法,但不知何故在getText1() 和getText2() 函数中写了+。那你也应该把这两个+换成*:

    Multiplication fiddle.

    【讨论】:

    • @ArghaGuhaRoy 如果通过“不工作”你正在谈论用 + 替换 * - 我已经在更新的小提琴中完成了这个。
    【解决方案3】:

    您没有 id 为“gt”的元素。

    把最后一行代码改成这样:

    document.getElementById('gt1').value = gt1;
    

    【讨论】:

    • @ArghaGuhaRoy 检查“摄政”的答案。重命名您的 gt1 函数。
    • @aleha 顺便说一句,我仍然很惊讶gt1 函数在有id="gt1" 时不会被执行
    【解决方案4】:

    您尚未定义“结果”变量 在 'gt1()' 中:

    document.getElementById('gt').value = gt1;
    

    1- 页面中没有 'gt' 元素

    2- 'gt1' 应该重命名为 'result' 最后的代码是:

    <script language="javascript" type="text/javascript">
        function getText1() {
            var in1 = document.getElementById('in1').value;
            var in2 = document.getElementById('in2').value;
            var tot1 = parseInt(in1) + parseInt(in2);
            document.getElementById('tot1').value = tot1;
        }
        function getText2() {
            var in1 = document.getElementById('in3').value;
            var in2 = document.getElementById('in4').value;
            var tot2 = parseInt(in1) + parseInt(in2);
            document.getElementById('tot2').value = tot2;
        }
    
        function gt1() {
            var in1 = document.getElementById('tot1').value;
            var in2 = document.getElementById('tot2').value;
            var result = parseInt(in1) + parseInt(in2);
            if (!isNaN(result)) {
                document.getElementById('gt1').value = result;
            }
            //document.getElementById('gt1').value=gt1;
        } 
    </script>
    

    这是正常的。

    【讨论】:

      【解决方案5】:

      检查下面的代码。

      <script >
      function getext3(){
          txt1 = document.getElementById("text1").value;
          txt2 = document.getElementById("text2").value;
          document.getElementById("text3").value = parseInt(txt1)*parseInt(txt2);
      
      } 
      
      function getext6(){
          txt1 = document.getElementById("text4").value;
          txt2 = document.getElementById("text5").value;
          document.getElementById("text6").value = parseInt(txt1)*parseInt(txt2);
      
      } 
      function getext7(){
      
      
          txt1 = document.getElementById("text3").value;
          txt2 = document.getElementById("text6").value;
          document.getElementById("text7").value = parseInt(txt1)+parseInt(txt2);
      
      } 
      </script>
      
      Text1 : <input type="text" id="text1" value="5"> <br/>
      Text2 : <input type="text" id="text2" value="2"> <br/>
      Text3 : <input type="text" id="text3" value="" onclick="getext3()"> <br/>
      
      
      Text4 : <input type="text" id="text4" value="7"> <br/>
      Text5 : <input type="text" id="text5" value="10"> <br/>
      Text6 : <input type="text" id="text6" value="" onclick="getext6()"> <br/>
      
      Text7 : <input type="text" id="text7" value="" onclick="getext7()"> <br/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多