简体   繁体   English

如何从作为动态行添加的一部分的字段中获得总计?

[英]How can I get a sum total from a field that is part of a dynamic row add?

I have a form, and the user can add as many rows as they want. 我有一个表单,用户可以根据需要添加任意多的行。 One of the fields in the dynamic row is total cost. 动态行中的字段之一是总成本。 I am trying to figure out how to add the entries for this one particular field and display them on the following page. 我试图弄清楚如何为该特定字段添加条目并将其显示在下一页上。

I am coding in html 5, coldfusion and javascript. 我正在用html 5,coldfusion和javascript进行编码。

Here is my script that generates the dynamic row: 这是生成动态行的脚本:

    <script>
    $(document).ready(function(){
        var counter = 1;
        $("#addmrow").click(function(event){
            event.preventDefault();
            var newRow = 
                '<tr id="rtr_' + counter + '">' +
                    '<td><input type="text" id="Qty_' + counter + '" name="Qty_' + counter + '" size=3/></td>' +
                    '<td><input type="text" id="Sku_' + counter + '" name="Sku_' + counter + '" size=10/></td>' +
                    '<td><input type="text" id="Description_' + counter + '" name="Description_' + counter + '" /></td>' +
                    '<td><input type="text" id="Retail_' + counter + '" name="Retail_' + counter + '" size=10/></td>' +
                    '<td><input type="text" id="TransferNumber_' + counter + '" name="TransferNumber_' + counter + '" size=10/></td>' +
                    '<td><input type="text" id="TransferDate_' + counter + '" name="TransferDate_' + counter + '" size=16/></td>' +
                '</tr>';
            counter++;
            $('#mlist').append(newRow);
        });
        $("#delmrow").click(function(event){
            event.preventDefault();
            counter--;
            $("#" + 'Qty_' + counter).remove();
            $("#" + 'Sku_' + counter).remove();
            $("#" + 'Description_' + counter).remove();
            $("#" + 'Retail_' + counter).remove();
            $("#" + 'TransferNumber_' + counter).remove();
            $("#" + 'TransferDate_' + counter).remove();
            $("#" + 'rtr_' + counter).remove();
        });
    });
</script>

Here is my code displaying it on the next page, but I cant figure out how to add the Average Cost field so I get a sum total of that field: 这是我的代码在下一页上显示,但是我无法弄清楚如何添加“平均成本”字段,因此我得到了该字段的总和:

                <cfloop index="i" list="#form.FieldNames#" delimiters=",">
                <cfif REFind("QTY_", i)>
                   <tr><td><cfoutput>#form[i]#</cfoutput></td>
                </cfif>
                <cfif REFind("SKU_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("DESCRIPTION_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("RETAIL_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("TRANSFERNUMBER_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("AVGCOST_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td></tr>
                </cfif> 
            </cfloop>

I hope I explained this correctly. 我希望我能正确解释。 Any help is much appreciated! 任何帮助深表感谢!

There are proably better ways to do this but given what you have this may be the simplest 可能有更好的方法来执行此操作,但是鉴于您所拥有的,这可能是最简单的方法

            <cfset avgTotal = 0>           
            <cfloop index="i" list="#form.FieldNames#" delimiters=",">
                <cfif REFind("QTY_", i)>
                   <tr><td><cfoutput>#form[i]#</cfoutput></td>
                </cfif>
                <cfif REFind("SKU_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("DESCRIPTION_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("RETAIL_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("TRANSFERNUMBER_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td>
                </cfif> 
                <cfif REFind("AVGCOST_", i)>
                   <td><cfoutput>#form[i]#</cfoutput></td></tr>
                    <cfset avgTotal = avgTotal+form[i]> 
                </cfif> 
            </cfloop>
            <cfoutput>#avgTotal#</cfoutput>

Is that what you are tying to do? 那是你想做的事吗? Or have I completely missed the mark 还是我完全错过了标记

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

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