简体   繁体   中英

two text box values sum into another text box in a table using javascript?

i tried a code for summing values of two text boxes and store it into another text box. The summing is working properly. but i didn't getting the result into a text box.please help me with the code snippet..

<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
    <script>
    $(document).ready(function(){
    $('#tablesum').find('input:text').each(function()       {$(this).keyup(function(){

            newSum.call(this);


        });
    });
});

function newSum() {
    var sum=0;
    var thisRow = $(this).closest('tr');

    var total=0;

    //iterate through each input and add to sum
    $(thisRow).find("td:not(.total) input").each(function() {

    console.log($(this).attr('id') );
            sum += parseInt(this.value);                     
    }); 
    //change value of total
    $(thisRow).find(".total").html(sum);


     // the grand total
     $('.total').each(function() {
         total += parseInt($(this).html());
     });

    $('.result').val(total);
}


    </script>

//html

<tr>
<td>&nbsp; &nbsp; &nbsp; i)   Full Time employees</td>
<td class="tdright total" ><input type=text id="2.1_1_1" name="2.1_1_1" title="qry"></td>
<td><input type=text id="2.1_1_2" name="2.1_1_2" title="qry"></td>
<td><input type=text id="2.1_1_3" name="2.1_1_3" title="qry"></td>
</tr>
saving two text box values and outputting the result in anothor,

http://jsfiddle.net/stanze/kxu22e3h/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM