0

大家好,目前我正在开发一个javascript函数,该函数需要在页脚行中显示gridview单元格的总和。现在我能够得到答案,但我无法将值保存在页脚行中。谁能帮我将值保存到gridview页脚标签。提前致谢。

这是我的js函数。

    function addTotal() {
        var input = document.getElementsByTagName("input");
        var Total = '0.0';
        var Sample;
        var val;
        for (var i = 0; i < input.length; i++) {
            if (input[i].type == "text") {
                if (input[i].id.indexOf("txtpercent") > 0) {
                    Sample = document.getElementById(input[i].id).value;
                    var val = parseFloat(Sample.replace(",", ""));

                    if (isNaN(val) == true) {
                        val = 0;
                    }

                    Total = parseFloat(Total) + val;
                    //document.getElementById("Flblallocationpercent").innerHTML=Total;
                }
            }
        }
        alert(Total);
        if (Total != 100) {
            alert("Allocation should be equal to 100 %")
            return false;
        }
    }

这是调用函数的设计部分

<asp:TemplateField HeaderText="Allocation Percentage">
                            <ItemTemplate>
                                        <asp:TextBox ID="txtpercent" runat="server" Text="" OnTextChanged="allocate_sum"
                                            Visible="true" ToolTip="Percentage" onblur="addTotal()" />

                            </ItemTemplate>
                            <FooterTemplate >

                                        <asp:Label ID="Flblallocationpercent" runat="server" Enabled="true" Width="95%" />

                            </FooterTemplate>
                            <FooterStyle HorizontalAlign="Center" Width="5%" />
                            <ItemStyle HorizontalAlign="Center" Width="5%" />
                        </asp:TemplateField>
4

2 回答 2

0

请注意下面提到的代码,将 cssclass 分配给文本框和标签。

<asp:TemplateField HeaderText="Allocation Percentage">
                            <ItemTemplate>
                                        <asp:TextBox ID="txtpercent" runat="server" Text="" Visible="true" ToolTip="Percentage" CssClass="txtAmount" onblur="addTotal()" />

                            </ItemTemplate>
                            <FooterTemplate >

                                        <asp:Label ID="Flblallocationpercent" runat="server" CssClass="TotalValue" ForeColor="Red" Width="95%" />

                            </FooterTemplate>
                            <FooterStyle HorizontalAlign="Center" Width="5%" />
                            <ItemStyle HorizontalAlign="Center" Width="5%" />
                        </asp:TemplateField>

在你的 head 部分添加以下 Javascript 函数,

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">                     

            function addTotal() {

                var Total = '0.0';
                var Sample;

                $(".txtAmount").each(function(){


            Total += parseFloat($(this).val());
            });

            alert(Total);
            $(".txtAmount").val(Total);
                    //OR
            $(".txtAmount").html(Total);
        }
</script>

并检查它是否能解决您的问题。希望能帮助到你

于 2013-05-28T04:52:28.313 回答
0

请参阅以下 Javascript 函数:

for (var i = 1; i < gridLength - 1; i++) {


                    sprice = grid.rows[i].cells[3].innerText;

                    sprice = sprice.replace(code, "");

                    sprice = sprice.replace(",", "");
                    if (sprice == "")
                        price = 0;
                    else
                        price = parseFloat(sprice);


                    subTotal = subTotal + parseFloat(price);

                }

通过链接,您还将获得参考背后的代码。

希望它有帮助。

于 2013-05-28T04:42:24.493 回答