本文介绍了如何从文本框这是在使用Javascript的GridView获取文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好我写了下面的脚本来从文本
这是 GridView控件的值
,但我我得到一些价值为 NaN的
可有人告诉我在哪里出了错
这是我的示例脚本
VAR格=的document.getElementById(<%= grdInvoice.ClientID%GT;);
VAR输入= grid.getElementsByTagName(输入);
对于(VAR I = 0; I< inputs.length;我++){
如果(输入[I] .TYPE ==文本){
如果(输入[I] .name.indexOf(txtAmount)。值!=){
警报(非空);
amnt = parseInt函数(输入[I] .name.indexOf(txtAmount)值。);
警报(amnt.toString()); //获取这里楠
//变种V =的document.getElementById('<%= lblTotal.ClientID%GT;')。值;
}
}
}
我的网格如下:
DDL数量递减率金额
1天10 10
2 D 20 40
像一些空行也将在那里我需要总结的金额
和显示器
我的网格视图
< ASP:GridView控件ID =grdInvoice=服务器的AutoGenerateColumns =FALSE网格=无
宽度=650像素CELLPADDING =2CELLSPACING =1前景色=#333333的CssClass =inv_grid_data
OnSelectedIndexChanged =grdInvoice_SelectedIndexChangedOnSelectedIndexChanging =grdInvoice_SelectedIndexChanging
风格=FONT-FAMILY:宋体; FONT-SIZE:9px;>
< RowStyle的BackColor =#f1f1f2前景色=#333333BORDERCOLOR =#333333边框=实
边框宽度=1px的/>
< HeaderStyle的CssClass =inv_grid_hed背景色=#f2f2f2前景色=黑字体,名称=宋体,宋体,黑体,无衬线
FONT-SIZE =9pxHEIGHT =15px的字体粗体=FALSE/>
< AlternatingRowStyle的CssClass =TR2背景色=白前景色=#284775/>
<柱体和GT;
< ASP:的TemplateField的HeaderText =项目名称HeaderStyle-WIDTH =140>
<&ItemTemplate中GT;
< ASP:DropDownList的ID =ddlItems=服务器的AutoPostBack =真OnSelectedIndexChanged =ddlItems_SelectedIndexChanged
DataTextField =LINEITEMNAMEDataValueField =LINEITEMNAME的CssClass =txt_box_ssm_drop
WIDTH =100%FONT-名称=宋体,宋体,黑体,无衬线FONT-SIZE =12像素>
< / ASP:DropDownList的>
< / ItemTemplate中>
< HeaderStyle WIDTH =180像素/>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =数量>
<&ItemTemplate中GT;
< ASP:文本框ID =txtQuantity的CssClass =txt_box_ssm_big=服务器WIDTH =100%>< / ASP:文本框>
< / ItemTemplate中>
< HeaderStyle宽度=100px的/>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =说明>
<&ItemTemplate中GT;
< ASP:文本框ID =txtDescription的CssClass =txt_box_ssm_big=服务器WIDTH =100%>< / ASP:文本框>
< / ItemTemplate中>
<% - < ItemStyle HEIGHT =11像素WIDTH =100像素/> - %GT;
< HeaderStyle宽度=260px/>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =率>
<&ItemTemplate中GT;
< ASP:文本框ID =txtRate只读=真的CssClass =txt_box_ssm_big=服务器
WIDTH =100%>< / ASP:文本框>
< / ItemTemplate中>
< HeaderStyle宽度=100px的/>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =金额>
<&ItemTemplate中GT;
< ASP:文本框ID =txtAmount=服务器的CssClass =txt_box_ssm_bigWIDTH =100%>< / ASP:文本框>
<% - < ASP:标签ID =lblamount=服务器文本='<%#的eval(额)%>/> - %GT;
< / ItemTemplate中>
< HeaderStyle宽度=100px的/>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =删除>
<&ItemTemplate中GT;
<% - < ASP:ImageButton的ID =ImageButton2=服务器的ImageUrl =〜/图片/ icon_delete.gif的OnClick =RowDelete_Click的OnClientClick =味精('你能在这里到达那里? 这是一个标题')/> - %GT;
< ASP:ImageButton的ID =RowDelete=服务器的ImageUrl =〜/图片/ icon_delete.gif
的OnClick =RowDelete_Click的OnClientClick =返回showConfirm()风格=保证金左:15px的; />
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:CommandField中ShowSelectButton =真按钮类型=形象SelectImageUrl =〜/ Invoiceimages /复制-32(1)的.png
SelectText =复制的HeaderText =复制/>
< /专栏>
< FooterStyle背景色=#5D7B9DFONT-粗体=真前景色=白/>
< PagerStyle背景色=#284775前景色=白HorizontalAlign =中心/>
< SelectedRowStyle背景色=#E2DED6FONT-粗体=真前景色=#333333/>
< EditRowStyle背景色=#999999/>
< / ASP:GridView的>
我原来的剧本最初我会根据用户的数量后网格视图中添加量我想在网格中显示的总金额将标签
<脚本类型=文/ JavaScript的>
功能乘法(txtQuantity,txtRate,txtAmount){
VAR重量=的document.getElementById(txtQuantity).value的;
VAR率=的document.getElementById(txtRate).value的;
的document.getElementById(txtAmount).value的=体重*率;
变种amnt = 0;
// VAR Grid_Table =的document.getElementById('<%= grdInvoice.ClientID%GT;');
//为(VAR行= 1;行< Grid_Table.rows.length;排++){//变种数量= 0;//为(VAR COL = 0;&山坳下,Grid_Table.rows [行] .cells.length;西++){// VAR cellcollectin = Grid_Table.rows [行] .cells [COL];//为(VAR J = 0; J< cellcollectin.childNodes.length; J ++){//如果(cellcollectin.childNodes [J]。类型==文本){//如果(cellcollectin.childNodes [J] .name.indexOf(txtAmount)> 1){
//如果(cellcollectin.childNodes [J] .value的!=){
//数量= parseInt函数(cellcollectin.childNodes [J]。价值);
// amnt = amnt +数量;
//}
//}
//}
//}
//}
//}
变种格=的document.getElementById(&下;%= grdInvoice.ClientID%gt;中);
VAR输入= grid.getElementsByTagName(输入);
对于(VAR I = 0; I< inputs.length;我++){
如果(输入[I] .TYPE ==文本){
如果(输入[I] .name.indexOf(txtAmount)。值!=){
警报(非空);
amnt = grid.rows [I] .cells [1] .childNodes [0] .value的;
// amnt = parseInt函数(输入[I] .name.indexOf(txtAmount)值。);
警报(amnt.toString());
//变种V =的document.getElementById('<%= lblTotal.ClientID%GT;')。值;
}
}
}
的document.getElementById('<%= lblTotal.ClientID%GT;')。innerHTML的= amnt.toString();
}
< / SCRIPT>
解决方案
都在这里后是解决方案
<脚本类型=文/ JavaScript的>
功能乘法(txtQuantity,txtRate,txtAmount){
VAR COL1;
变种totalcol1 = 0;
VAR重量=的document.getElementById(txtQuantity).value的;
VAR率=的document.getElementById(txtRate).value的;
的document.getElementById(txtAmount).value的=体重*率;
VAR格=的document.getElementById('<%= grdInvoice.ClientID%GT;');
对于(i = 0; I< grid.rows.length;我++){
COL1 = grid.rows [I] .cells [4];
// COL2 = grid.rows [Ⅰ] .cells [1]; 为(J = 0; J< col1.childNodes.length; J ++){
如果(col1.childNodes [J]。类型==文本){
如果(isNaN(col1.childNodes [J]。价值)及!&安培;!col1.childNodes [J]。价值=){
totalcol1 + = parseInt函数(col1.childNodes [J]。价值)
}
}
}
}
的document.getElementById('&下;%= lblTotal.ClientID%GT;')。innerHTML的= totalcol1.toFixed(2)的ToString();
}
< / SCRIPT>
Hi all I have written the following script to get the value from the textbox
which is in GridView
but I am getting some value as NaN
can some one tell where I went wrong
This is my sample script
var grid = document.getElementById("<%=grdInvoice.ClientID%>");
var inputs = grid.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "text") {
if (inputs[i].name.indexOf("txtAmount").value != "") {
alert("Not Null");
amnt = parseInt(inputs[i].name.indexOf("txtAmount").value);
alert(amnt.toString()); // Getting Nan here
//var v = document.getElementById('<%= lblTotal.ClientID %>').value;
}
}
}
My grid is as follows
ddl Quantity desc Rate Amount
1 d 10 10
2 d 20 40
Like that some empty rows will also be there I need to sum the Amount
and display
My grid view
<asp:GridView ID="grdInvoice" runat="server" AutoGenerateColumns="False" GridLines="None"
Width="650px" CellPadding="2" CellSpacing="1" ForeColor="#333333" CssClass="inv_grid_data"
OnSelectedIndexChanged="grdInvoice_SelectedIndexChanged" OnSelectedIndexChanging="grdInvoice_SelectedIndexChanging"
Style="font-family: verdana; font-size: 9px;">
<RowStyle BackColor="#f1f1f2" ForeColor="#333333" BorderColor="#333333" BorderStyle="Solid"
BorderWidth="1px" />
<HeaderStyle CssClass="inv_grid_hed" BackColor="#f2f2f2" ForeColor="black" Font-Names="Verdana,Arial,Helvetica,sans-serif"
Font-Size="9px" Height="15px" Font-Bold="false" />
<AlternatingRowStyle CssClass="tr2" BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Item Name" HeaderStyle-Width="140">
<ItemTemplate>
<asp:DropDownList ID="ddlItems" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlItems_SelectedIndexChanged"
DataTextField="LineItemName" DataValueField="LineItemName" CssClass="txt_box_ssm_drop"
Width="100%" Font-Names="Verdana,Arial,Helvetica,sans-serif" Font-Size="12px">
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle Width="180px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" CssClass="txt_box_ssm_big" runat="server" Width="100%"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox ID="txtDescription" CssClass="txt_box_ssm_big" runat="server" Width="100%"></asp:TextBox>
</ItemTemplate>
<%--<ItemStyle Height="11px" Width="100px" /> --%>
<HeaderStyle Width="260px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Rate">
<ItemTemplate>
<asp:TextBox ID="txtRate" ReadOnly="true" CssClass="txt_box_ssm_big" runat="server"
Width="100%"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:TextBox ID="txtAmount" runat="server" CssClass="txt_box_ssm_big" Width="100%"></asp:TextBox>
<%--<asp:Label ID="lblamount" runat="server" Text='<%# Eval("Amount") %>'/>--%>
</ItemTemplate>
<HeaderStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<%--<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/icon_delete.gif" OnClick="RowDelete_Click" OnClientClick="msg('Can you get there from here?','This is a Title')"/>--%>
<asp:ImageButton ID="RowDelete" runat="server" ImageUrl="~/images/icon_delete.gif"
OnClick="RowDelete_Click" OnClientClick="return showConfirm()" Style="margin-left: 15px;" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="true" ButtonType="image" SelectImageUrl="~/Invoiceimages/Copy-32(1).png"
SelectText="Copy" HeaderText="Copy" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
</asp:GridView>
My original script initially I will add AMount in the grid view based on user quantity after that I Would like to display the total amount in the grid to a label
<script type="text/javascript">
function multiplication(txtQuantity, txtRate, txtAmount) {
var weight = document.getElementById(txtQuantity).value;
var rate = document.getElementById(txtRate).value;
document.getElementById(txtAmount).value = weight * rate;
var amnt = 0;
// var Grid_Table = document.getElementById('<%= grdInvoice.ClientID %>');
// for (var row = 1; row < Grid_Table.rows.length; row++) {
// var qty = 0;
// for (var col = 0; col < Grid_Table.rows[row].cells.length; col++) {
// var cellcollectin = Grid_Table.rows[row].cells[col];
// for (var j = 0; j < cellcollectin.childNodes.length; j++) {
// if (cellcollectin.childNodes[j].type == "text") {
// if (cellcollectin.childNodes[j].name.indexOf("txtAmount") > 1) {
// if (cellcollectin.childNodes[j].value != "") {
// qty = parseInt(cellcollectin.childNodes[j].value);
// amnt = amnt + qty;
// }
// }
// }
// }
// }
// }
var grid = document.getElementById("<%=grdInvoice.ClientID%>");
var inputs = grid.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "text") {
if (inputs[i].name.indexOf("txtAmount").value != "") {
alert("Not Null");
amnt = grid.rows[i].cells[1].childNodes[0].value;
//amnt = parseInt(inputs[i].name.indexOf("txtAmount").value);
alert(amnt.toString());
//var v = document.getElementById('<%= lblTotal.ClientID %>').value;
}
}
}
document.getElementById('<%= lblTotal.ClientID %>').innerHtml = amnt.toString();
}
</script>
解决方案
After all here is the solution
<script type="text/javascript">
function multiplication(txtQuantity, txtRate, txtAmount) {
var col1;
var totalcol1 = 0;
var weight = document.getElementById(txtQuantity).value;
var rate = document.getElementById(txtRate).value;
document.getElementById(txtAmount).value = weight * rate;
var grid = document.getElementById('<%=grdInvoice.ClientID %>');
for (i = 0; i < grid.rows.length; i++) {
col1 = grid.rows[i].cells[4];
//col2 = grid.rows[i].cells[1];
for (j = 0; j < col1.childNodes.length; j++) {
if (col1.childNodes[j].type == "text") {
if (!isNaN(col1.childNodes[j].value) && col1.childNodes[j].value != "") {
totalcol1 += parseInt(col1.childNodes[j].value)
}
}
}
}
document.getElementById('<%= lblTotal.ClientID %>').innerHTML = totalcol1.toFixed(2).toString();
}
</script>
这篇关于如何从文本框这是在使用Javascript的GridView获取文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!