两个文本框值使用javascript汇总到表格中的另一个文本框?

时间:2015-04-06 04:33:44

标签: javascript html sum

我尝试了一个代码,用于对两个文本框的值进行求和,并将其存储到另一个文本框中。求和工作正常。但是我没有把结果放到一个文本框中。请帮我处理代码片段。

<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>

1 个答案:

答案 0 :(得分:0)

saving two text box values and outputting the result in anothor,

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

相关问题