Add two textbox values and display the sum in a third textbox automatically

后端 未结 8 2005
一整个雨季
一整个雨季 2020-12-18 00:43

I have assigned a task to add two textbox values.I want the result of addition to appear in the 3rd textbox,as soon as enter the values in the first two textboxes,without pr

相关标签:
8条回答
  • 2020-12-18 00:56

    try this

      function sum() {
           var txtFirstNumberValue = document.getElementById('txt1').value;
           var txtSecondNumberValue = document.getElementById('txt2').value;
           if (txtFirstNumberValue == "")
               txtFirstNumberValue = 0;
           if (txtSecondNumberValue == "")
               txtSecondNumberValue = 0;
    
           var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue);
           if (!isNaN(result)) {
               document.getElementById('txt3').value = result;
           }
       }
    
    0 讨论(0)
  • 2020-12-18 01:00

    In below code i have done operation of sum and subtraction: because of using JavaScript if you want to call function, then you have to put your below code outside of document.ready(function{ }); and outside the script end tag.

    I have taken one another script tag for this operation.And put below code between script starting tag // your code // script ending tag.

     function operation() 
    
     {
    
       var txtFirstNumberValue = parseInt(document.getElementById('basic').value);
       var txtSecondNumberValue =parseInt(document.getElementById('hra').value);
       var txtThirdNumberValue =parseInt(document.getElementById('transport').value);
       var txtFourthNumberValue =parseInt(document.getElementById('pt').value);
       var txtFiveNumberValue = parseInt(document.getElementById('pf').value);
    
       if (txtFirstNumberValue == "")
           txtFirstNumberValue = 0;
       if (txtSecondNumberValue == "")
           txtSecondNumberValue = 0;
       if (txtThirdNumberValue == "")
           txtThirdNumberValue = 0;
       if (txtFourthNumberValue == "")
           txtFourthNumberValue = 0;
       if (txtFiveNumberValue == "")
           txtFiveNumberValue = 0;
    
    
    
       var result = ((txtFirstNumberValue + txtSecondNumberValue + 
      txtThirdNumberValue) - (txtFourthNumberValue + txtFiveNumberValue));
       if (!isNaN(result)) {
           document.getElementById('total').value = result;
       }
     }
    

    And put onkeyup="operation();" inside all 5 textboxes in your html form. This code running in both Firefox and Chrome.

    0 讨论(0)
  • 2020-12-18 01:06

    This is the correct code

    function sum() 
    { 
        var txtFirstNumberValue = document.getElementById('total_fees').value; 
        var txtSecondNumberValue = document.getElementById('advance_payement').value; 
        var result = parseInt(txtFirstNumberValue) - parseInt(txtSecondNumberValue); 
        if(txtFirstNumberValue=="" ||txtSecondNumberValue=="") 
        { 
            document.getElementById('balance_payement').value = 0; 
        }
        if (!isNaN(result)) 
        { 
            document.getElementById('balance_payement').value = result;
        }
    } 
    
    0 讨论(0)
  • 2020-12-18 01:10

    Try this: Open given fiddle in CHROME

    function sum() {
          var txtFirstNumberValue = document.getElementById('txt1').value;
          var txtSecondNumberValue = document.getElementById('txt2').value;
          var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue);
          if (!isNaN(result)) {
             document.getElementById('txt3').value = result;
          }
    }
    

    HTML

    <input type="text" id="txt1"  onkeyup="sum();" />
    <input type="text" id="txt2"  onkeyup="sum();" />
    <input type="text" id="txt3" />
    

    DEMO HERE

    0 讨论(0)
  • 2020-12-18 01:10

    i didn't find who made elegant answer , that's why let me say :

    Array.from(
       document.querySelectorAll('#txt1,#txt2')
    ).map(e => parseInt(e.value) || 0) // to avoid NaN
    .reduce((a, b) => a+b, 0)
    

    window.sum= () => 
     document.getElementById('result').innerHTML=    
       Array.from(
         document.querySelectorAll('#txt1,#txt2')
       ).map(e=>parseInt(e.value)||0)
       .reduce((a,b)=>a+b,0)
    <input type="text" id="txt1" onkeyup="sum()"/>
    <input type="text" id="txt2" onkeyup="sum()"  style="margin-right:10px;"/><span id="result"></span>

    0 讨论(0)
  • 2020-12-18 01:15

    well I think the problem solved this below code works:

    function sum() {
        var result=0;
           var txtFirstNumberValue = document.getElementById('txt1').value;
           var txtSecondNumberValue = document.getElementById('txt2').value;
        if (txtFirstNumberValue !="" && txtSecondNumberValue ==""){
            result = parseInt(txtFirstNumberValue);
        }else if(txtFirstNumberValue == "" && txtSecondNumberValue != ""){
            result= parseInt(txtSecondNumberValue);
        }else if (txtSecondNumberValue != "" && txtFirstNumberValue != ""){
            result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue);
        }
           if (!isNaN(result)) {
               document.getElementById('txt3').value = result;
           }
       }
    
    0 讨论(0)
提交回复
热议问题