如何通过JavaScript从GridView获取Text值?
我尝试了这个,但它没有显示Text =“这是一个标签”的值:
var lblThis = document.getElementById('<%= GridView1.ClientID %>')
var lblThisTag = lblThis.document.getElementByTag('txtValueResultMandays').innerHTML
alert(lblThisTag);
样品:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Mandays">
<ItemTemplate>
<asp:Label ID="txtValueResultMandays" runat="server" Text="this is a label"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
感谢。
答案 0 :(得分:0)
var lblThis = document.getElementById('<%= GridView1.ClientID %>')
if(grid.rows.length>0){
//loop starts from because first row is columnNames
for (i = 1; i < grid.rows.length-1; i++) {
var label= grid.rows[i].cells[index of the cell with label].elements[0];
alert(label.InnerHTML);
}
}
你应该尝试这样的事情。
答案 1 :(得分:0)
如果有ID,为什么不尝试使用jquery直接访问标签值,如下所示。
var value = $('#<%=txtValueResultMandays.ClientID%>').val();
或尝试使用getElementById方法直接在java脚本中访问它,而不是通过网格。