嘿伙计们我正在尝试添加在动态网格视图中输入的值,并使用javascript在gridview外面的文本框中显示总和..以下是我尝试的代码...当我输入任何值时我在textbox中只显示0。我希望总和显示在文本框中。你们可以告诉我哪里出错了吗?
我的javascript代码
<script type="text/javascript">
function showCal(obj){
var table = document.getElementById("Gridview1");
var sum = 0;
for (var i = 1; i < table.rows.length; i++)
{
if (isNaN(table.rows[i].cells[1].innerText != null))
{
if (!isNaN(table.rows[i].cells[1].innerText))
{
sum = (parseFloat(sum) + parseFloat(table.rows[i].cells[1].innerText)).toString();
}
}
document.getElementById("TextBox4").innerText = sum;
}
}
我的ASP.NET代码
PagerStyle模式= “NumericPages”
AutoGenerateColumns="false" onrowcreated="Gridview1_RowCreated" Height="16px">
<PagerStyle CssClass="pgr" Height="25px" BorderStyle="Solid" />
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Serial Number" />
<asp:TemplateField HeaderText="Air/Bus/Train Fare">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" CssClass="txtBoxNormalmedium" onkeyup="showCal(this);" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City To">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" CssClass="txtBoxNormalmedium" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City From">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" CssClass="txtBoxNormalmedium"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
onclick="ButtonAdd_Click" CssClass="btnNormalAdd" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" CssClass="lnkbut">Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
<asp:TextBox ID="TextBox4" runat="server" onkeyup="showCal(this);" ></asp:TextBox>
答案 0 :(得分:0)
此代码与您的代码类似:
var sum = 0;
var cell = 5.5;
if (isNaN(cell != null)) {
if (!isNaN(cell)) {
sum = sum + cell;
}
}
console.log(sum);
应删除第一个isNaN()检查:
if (cell != null) {
if (!isNaN(cell)) {