循环 ID 和每个函数

发布于 2024-10-27 11:40:46 字数 896 浏览 2 评论 0原文

我使用 JQUERY EACH 函数通过以下代码对“txt”类动态求和 12 个文本框值:

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

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

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

    });

    function calculateSum() {

        var sum = 0;
        //iterate through each textboxes and add the values
        $(".txt").each(function() {

            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
                }

        });

        $("#sum").html(sum);
</script>

我想创建 4 个仅使用 3 个文本框值动态计算的小计。我在表单中为每个 ID 添加了 4 个不同的 ID。我如何使用上面的函数动态计算当前 id 的小计。得到结果的spans id被命名为“ID_SUM”(ID必须根据修改的文本框的ID值动态变化)? 非常感谢你。

I use the JQUERY EACH function to SUM dynamically 12 textbox values with a "txt" class with this code :

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

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

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

    });

    function calculateSum() {

        var sum = 0;
        //iterate through each textboxes and add the values
        $(".txt").each(function() {

            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
                }

        });

        $("#sum").html(sum);
</script>

I want to create 4 subtotals computed dynamically using each only 3 textbox values. I added 4 different ID to each in my form. How can i use the function above to compute dynamically the subtotal for the current id. The spans id which gets the result are named "ID_SUM" (ID must be dynamic according to the ID value of the textbox modified) ??
Thank u very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2024-11-03 11:40:46

我找到了一个解决方案... ID 是唯一的,所以它不起作用。我使用这样的名称属性:

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

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

            $(this).keyup(function(){
            calculateSum($(this).attr("name"));
            });
        });

    });

    function calculateSum(groupe) {

        var sum = 0;
        var subtotal = 0
        //iterate through each textboxes and add the values
        $(".txt").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 : $("#sum").html(sum.toFixed(2))
        $("#sum").html(sum);

        $('input[name="' + groupe + '"]').each(function() {

            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                subtotal += parseFloat(this.value);
                }

        });
        //.toFixed() method will roundoff the final sum to 2 decimal places : $("#sum").html(sum.toFixed(2))
        $("#" + groupe + "_SUM").html(subtotal);

    }
</script>

i found a solution... ID is unique so it doesn't work. i used the name attribute like this :

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

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

            $(this).keyup(function(){
            calculateSum($(this).attr("name"));
            });
        });

    });

    function calculateSum(groupe) {

        var sum = 0;
        var subtotal = 0
        //iterate through each textboxes and add the values
        $(".txt").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 : $("#sum").html(sum.toFixed(2))
        $("#sum").html(sum);

        $('input[name="' + groupe + '"]').each(function() {

            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                subtotal += parseFloat(this.value);
                }

        });
        //.toFixed() method will roundoff the final sum to 2 decimal places : $("#sum").html(sum.toFixed(2))
        $("#" + groupe + "_SUM").html(subtotal);

    }
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文