• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在按键事件脚本上显示其他文本框的总和

用户头像
it1352
帮助1

问题说明

我有三个或更多动态创建并具有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>

正确答案

#1
(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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcgkhee
系列文章
更多 icon
同类精品
更多 icon
继续加载