简体   繁体   English

追加更多输入字段后的jQuery计算

[英]jQuery Calculations after appending more input fields

I am building an expense template an am having an issue regarding using jQuery Calculations and click functionality to append a set of input fields. 我正在建立一个费用模板,关于使用jQuery计算和单击功能来附加一组输入字段时遇到问题。

I am combining twooncodes, one to calculate the sum of the values in input fields, and the other to Add a new row of input fields when the user clicks so (these are also to be added to the sum). 我正在组合两个代码,一个用于计算输入字段中值的总和,另一个用于在用户单击时添加新的输入字段行(这些也将添加到总和中)。 Problem is, the sum is not adding to the total from the newly appended rows. 问题是,总和没有添加到新添加的行的总数中。 Only the default row of fields adds. 仅添加默认的字段行。

Any help is appreciated (Fiddle ex: http://jsfiddle.net/NicoleScotsburn/o8x02sjh/4/ ) 感谢您的任何帮助(Fiddle前: http : //jsfiddle.net/NicoleScotsburn/o8x02sjh/4/

Appending table with inputs code: 附有输入代码的表:

            //Increment Variables
            var items = $('#items');
            var i = $('#items td').size() + 1;
            var mileage = $('#mileage');
            var j = $('#mileage td').size() + 1;

            //Append Table Rows
            $('#addItem').on('click', function() {
                    $('<tr><td>&nbsp;<label for="date"><input type="text" id="date" size="10" name="date_' + i +'" value="" placeholder="mm/dd/yyyy" /></label></td>' +
                      '<td>&nbsp;<label for="desc"><input type="text" id="desc" size="30" name="desc_' + i +'" /></label></td>' +
                      '<td>&nbsp;$<label for="entmt"><input type="text" id="sum" size="10" name="entmt_' + i +'" placeholder="0.00" /></label></td>' +
                      '<td>&nbsp;$<label for="meals"><input type="text" id="sum" size="10" name="meals_' + i +'" placeholder="0.00" /></label></td>' +
                      '<td>&nbsp;$<label for="other"><input type="text" id="sum" size="10" name="other_' + i +'" placeholder="0.00" /></label></td>' +
                      '<td>&nbsp;$<label for="travel"><input type="text" id="sum" size="10" name="travel_' + i +'" placeholder="0.00" /></label></td>' +
                      '<td>&nbsp;$<label for="lodging"><input type="text" id="sum" size="10" name="lodging_' + i +'" placeholder="0.00" /></label></td>' +
                      '<td>&nbsp;$<label for="hst"><input type="text" id="sum" size="10" name="hst_' + i +'" placeholder="0.00" /></label></td>' +
                      '<td>&nbsp;<a href="#" id="remItem">Remove</a></td></tr>').appendTo(items);
                    i++;
                    return false;
            });

            $('#addMileage').on('click', function() {
                    $('<tr><td>&nbsp;<label for="mdate"><input type="text" id="mdate" size="10" name="mdate' + j +'" value="" placeholder="mm/dd/yyyy" /></label></td>' +
                      '<td>&nbsp;<label for="mlocation"><input type="text" id="mlocation" size="30" name="mlocation' + j +'" value="" placeholder="" /></label></td>' +
                      '<td>&nbsp;<label for="km"><input type="text" id="km" size="10" name="km' + j +'" value="" placeholder="" />KM</label></td>' +
                      '<td>&nbsp;<a href="#" id="remMileage">Remove</a></td></tr>').appendTo(mileage);
                    j++;
                    return false;
            });


            //Remove Buttons
            $('body').on('click', '#remItem', function() { 
                    if( i > 2 ) {
                            $(this).parents('tr').remove();
                            i--;
                    }
                    return false;
            });
            $('body').on('click', '#remMileage', function() { 
                if( j > 2 ) {
                        $(this).parents('tr').remove();
                        j--;
                }
                return false;
             });

Calculation function: (This works assuming the id of the input is id="sum" and gets outputted to another input called totalsum. 计算函数:(这在假设输入的id为id =“ sum”的情况下起作用,并输出到另一个称为totalsum的输入中。

        $("input[id^=sum]").sum("keyup", "#totalSum");

I am not familiar with jQuery Calculations, but it looks like what you are doing can be achieved using plain jQuery or javascript. 我对jQuery Calculations并不熟悉,但是看起来您可以使用简单的jQuery或javascript来实现。 I did a quick google search for jquery sum and I found this other stackoverflow post that might help you. 我做了一个快速的谷歌搜索的jQuery总和,我发现这可能会帮助您的其他stackoverflow文章。 stackoverflow sum 堆栈溢出总和

You can add a class attribute called "sum" to all the fields you want to sum up and use the jquery marked as the answer. 您可以在要汇总的所有字段中添加一个名为“ sum”的类属性,并使用标记为答案的jquery。 Once you get done calculating the total you can assign it to the total amount input field. 完成总计计算后,您可以将其分配到总计输入字段。

$('.sum').blur(function () {
    var sum = 0;
    $('.sum').each(function() {
       sum += Number($(this).val());
    });

    $("#totalsum").val(sum);
});​​​​​​​​​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM