compute sum dynamically with javascript

后端 未结 5 1919
耶瑟儿~
耶瑟儿~ 2021-01-22 20:57

I have two textboxes Num1 and Num2 and another textbox Sum having the value 10.

How can I do this wherein if the user

相关标签:
5条回答
  • 2021-01-22 21:20
    <!DOCTYPE html>
    <html>
    <head>
    
        <script type="text/javascript">
    function calculate(){
        var x=parseInt(document.getElementById("first").value);
        var y=parseInt(document.getElementById("second").value);
        document.getElementById("answer").value=(x+y);
        }
        </script>
    
        <title>exam</title>
    </head>
    
    <body>
        <form>
            First:<input id="first" name="first" type="text"><br>
            Second:<input id="second" name="second" type="text"><br>
            Answer:<input id="answer" name="answer" type="text"><br>
            <input onclick="calculate()" type="button" value="Addition">
        </form>
    </body>
    </html>
    
    0 讨论(0)
  • 2021-01-22 21:25

    Something like that:

    <input type="text" id="Num1" value="1" onblur="recalculateSum();"/>
    <span>+</span>
    <input type="text" id="Num2" value="1" onblur="recalculateSum();"/>
    <span>=</span>
    <input type="text" id="Sum" value=""/>
    <script>
    
        function recalculateSum()
        {
            var num1 = parseInt(document.getElementById("Num1").value);
            var num2 = parseInt(document.getElementById("Num2").value);
            document.getElementById("Sum").value = num1 + num2;
    
        }
    
    </script>
    
    0 讨论(0)
  • 2021-01-22 21:31
    <input type="text" id="Num1" name="Num1"/>
    <input type="text" id="Num2" name="Num2"/>
    <input type="text" id="Sum" name="Sum"/>
    
    
    function addNums() {
      var num1 = parseInt(document.getElementById('Num1').value,10);
      var num2 = parseInt(document.getElementById('Num2').value,10)
      document.getElementById('Sum').value = (num1 + num2).toString();
    }
    

    There are other ways to reference the form items, but this one works.

    0 讨论(0)
  • 2021-01-22 21:34
    <input type="text" id="Num1" value="1" onblur="recalculateSum();"/>
    
    <input type="text" id="Num2" value="1" onblur="recalculateSum();"/>
    
    <input type="text" id="Sum" value=""/>
    <script>
    
        function recalculateSum()
        {
            var num1 = parseInt(document.getElementById("Num1").value);
            var num2 = parseInt(document.getElementById("Num2").value);
            document.getElementById("Sum").value = num1 + num2;
    
        }
    
    </script>
    
    0 讨论(0)
  • 2021-01-22 21:40

    Another version.

    
    <!DOCTYPE html>
    <html>
    <head>
    <title>Summer</title>
    </head>
    <body>
      Num 1: <input type="text" id="num1" name="num1" value="4"/><br />
      Num 2: <input type="text" id="num2" name="num2" value="6"/><br />
      Sum    <input type="text" id="sum"  name="sum" value="10">
    
    <script type="text/javascript">
      var _num1 = document.getElementById('num1');
      var _num2 = document.getElementById('num2');
      var _sum  = document.getElementById('sum');
    
      _num1.onblur = function(){
        _sum.value = (parseInt(_sum.value,10) + parseInt(this.value,10));
      };
      _num2.onblur = function(){
        _sum.value = (parseInt(_sum.value,10) + parseInt(this.value,10));
      };  
    </script>
    </body>
    </html>
    
    
    
    
    0 讨论(0)
提交回复
热议问题