![](/img/trans.png)
[英]While calculating total of gridview cell using javascript and jquery getting error
[英]Calculating gridview field total and displaying in footer text box using javascript
我正在努力找出 gridview 單元格的總數並顯示值頁腳文本框。 每次我遇到異常時:請幫助我做錯了什么:
這是例外: Microsoft JScript 運行時錯誤:預期 Object *這是我的 gridview 代碼: *
<div>
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true"
onrowcommand="Gridview1_RowCommand" AutoGenerateColumns="false"
CellSpacing="0" CellPadding="0" Font-Bold="false"
onrowdeleting="Gridview1_RowDeleting">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number"/>
<asp:TemplateField HeaderText="Select" ControlStyle-Width="50px" HeaderStyle-Font-Bold="false" ControlStyle-Font-Bold="false">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" Width="80px"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 1" HeaderStyle-Font-Bold="false" ControlStyle-Font-Bold="false">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Width="70px"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" Text="Total" Font-Bold="true"> </asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 2" HeaderStyle-Font-Bold="false" ControlStyle-Font-Bold="false">
<ItemTemplate>
<asp:TextBox ID="TextBox2" Width="70px" runat="server" onkeyup="Calculate('Gridview1')"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="total" runat="server" Width="70px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 3" HeaderStyle-Font-Bold="false" ControlStyle-Font-Bold="false">
<ItemTemplate>
<asp:TextBox ID="TextBox3" Width="70px" runat="server" ></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" CommandName="AddNewRow" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
</div>
這是我的 JavaScript 代碼:
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.5.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/jquery-ui.js"></script>
<script type="text/javascript">
function Calculate(GridView)
{
var total = 0;
var gridview = document.getElementById('<%=Gridview1.ClientID %>').getElementsByTagName("input");
for ( i = 0; i < gridview.rows.length; i ++)
{
var node = gridview.rows[i].cells[3].childNodes[3]; //textbox
if (node != undefined && node.type == "text") //check only textbox, ignore empty one
if (!isNaN(node.value) && node.value != "") //check for valid number
total += parseInt(node.value);
}
// document.getElementById("total").innerHTML = total.toString(); //display
var gridview1 = document.getElementById('<%=grdview1.ClientID %>');
gridview1.rows[gridview.rows.length -1].cells[0].innerHTML=total;
}
我嘗試了很多方法,但每次都遇到相同的異常:Microsoft JScript runtime error: Object expected
我的 JavaScript 代碼似乎有問題..請有人幫幫我。
Your function doesn't need to take in an object since you are not using it in your javascript, the way you are passing the GridView I don't believe would work anyway.
我認為,如果您利用一些 jQuery 選擇器在 DOM 中查找您需要的元素,我認為您的 javascript 會更干凈,也更容易遵循。 您可以對要總計的文本框使用特殊的 class ,例如:
....
<asp:TemplateField HeaderText="Header 1" HeaderStyle-Font-Bold="false" ControlStyle-Font-Bold="false">
<ItemTemplate>
<asp:TextBox ID="TextBox1" Class="TotalBox" runat="server" Width="70px"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" Class="TotalLabel" runat="server" Text="Total" Font-Bold="true"> </asp:Label>
</FooterTemplate>
</asp:TemplateField>
....
Java腳本
<script type="text/javascript">
function Calcutate()
{
var total = 0;
$('.TotalBox').each(function() {
total = parseInt(this.val()) + total;
});
$('.TotalLabel').html(total);
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.