我在asp.net中有一个gridview 其中一个字段是 gridview 内部的隐藏字段:
<asp:TemplateField>
<ItemTemplate>
<input type="hidden" value="0" id="hdnIsChanged" runat="server" />
</ItemTemplate>
</asp:TemplateField>
我在 GridView 内也有一个单选按钮列表,其中有一个正在运行的 jquery 单击事件... 这是该事件:
$("#MainContent_gvLineItems input[id*='rbAnswer']").click(function () {
var parentRow = $(this).parents('tr').eq(1) //used to get the row at index 1, parents('tr').length prints 3.
//tr around the checkbox is index 2
//tr around row is index 1
//tr around header is index 0
//so we want to get a reference to index=1
var firstCell = parentRow.find('td:eq(0)'); //find the first cell
var p = $(this).parents("div[id='dMainAnswer']").find(".Answer:first"); //used to find the panel
var val = $(this).val();
switch (val) //check the value
{
case 'No':
firstCell.css('background-color', 'red');
p.show();
break;
case 'Yes':
firstCell.css('background-color', 'green');
p.hide();
break;
case 'N/A':
firstCell.css('background-color', 'gray');
p.hide();
break;
default:
firstCell.css('background-color', 'transparent');
p.show();
break;
}
});
这一切都很好,但是在这个点击事件中我想访问隐藏字段hdnIsChanged
我如何引用它?我尝试过:
alert($('input[id$=hdnAnswered').val());
但它仍然显示未定义... 我希望能够在此点击事件中访问它并使用jquery为其设置一个值。 请记住它位于 gridview 内,因此它会出现在每一行中...
感谢任何帮助。
最佳答案
我个人会为您的隐藏字段分配一个类,并尝试像这样访问它:
$(this).closest(".myClass");
请记住,由于这是一个服务器控件,因此该隐藏字段的 ID 很可能会以一堆 asp.net 垃圾开头。所以不要像这样渲染:
<input type="hidden" value="0" id="hdnIsChanged" />
它很可能会像这样呈现:
<input type="hidden" value="0" id="clt100_clt100_290420349823049823423_hdnIsChanged" />
关于javascript - 使用 jquery 访问 gridview 内的隐藏字段并更新它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12097003/