在按键事件脚本上显示其他文本框中的总和 [英] On key press event script to display sum in other textbox

查看:94
本文介绍了在按键事件脚本上显示其他文本框中的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个或更多动态创建并具有ID的文本框。我必须显示文本框中所有值的总和,并按下按键上的总和ID。如果文本框为空,则不应将其计入总和中。如果我们在两个文本框中给出值,它会将两个文本框值的总和显示到我的文本框中,该文本框具有id sum。问题是,这有效,但没有给我的文本框正确的值和总和。



我尝试过:



我的剧本

I have three or more textboxes which are created dynamically and have ids. I have to show the sum of all the values in my textbox with the sum id on key press. If a textbox is empty, it should not be accounted in the sum. If we give the value in two textbox than it will display the sum of two textbox value to my textbox which has id sum. The problem is that this works but doesn't give the correct value to my textbox with the sum id.

What I have tried:

my script

<script>
    $(document).ready(function () {

        //iterate through each textboxes and add keyup
        //handler to trigger sum event
        $(".form-control").each(function () {

            $(this).keyup(function () {
                calculateSum();
            });
        });

    });
    function calculateSum() {
        debugger;
        var sum = 0;
        //iterate through each textboxes and add the values
        $(".form-control").each(function () {
            //add only if the value is number
            if (!isNaN(this.value) && this.value.length != 0) {
                sum += parseFloat(this.value);

            }

        });
        //.toFixed() method will roundoff the final sum to 2 decimal places
        // int sums = sum;
        //jQuery.noConflict("Debit").value = sums;
        //$("#Debit").val(sum);
        //$('#sum').html(sum.toFixed(2));
        document.getElementById('sum').value = sum;

    }
</script>
the textbox where to display sum
<div class="form-group">
                                        <label class="control-label col-md-3">
                                            Debitt:
                                        </label>
                                        <div class="col-md-4">
                                            <input type="text" name="Debit" id="sum" class="form-control"><br>
                                        </br></div>
                                        <span id="errorName" style="color: Red;"></span>
                                    </div>

推荐答案

(document).ready(function(){

// 它通过每个文本框进行操作并添加keyup
// 处理程序以触发sum事件
(document).ready(function () { //iterate through each textboxes and add keyup //handler to trigger sum event


.form-control)。每个( function(){
(".form-control").each(function () {


this )。keyup(function(){
calculateSum();
});
});

});
函数calculateSum(){
调试器;
var sum = 0 ;
// 遍历每个文本框并添加值
(this).keyup(function () { calculateSum(); }); }); }); function calculateSum() { debugger; var sum = 0; //iterate through each textboxes and add the values


这篇关于在按键事件脚本上显示其他文本框中的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆