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

后端 未结 8 2041
一整个雨季
一整个雨季 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 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;
           }
       }
    

提交回复
热议问题