簡體   English   中英

使用javascript,兩個文本框值求和成表中的另一個文本框?

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

我嘗試了將兩個文本框的值求和並將其存儲到另一個文本框中的代碼。 匯總工作正常。 但我沒有將結果輸入文本框。請幫助我提供代碼段。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM