我在asp.net上有一个gridview 其中一个字段是gridview内部的隐藏字段:
<asp:TemplateField>
<ItemTemplate>
<input type="hidden" value="0" id="hdnIsChanged" runat="server" />
</ItemTemplate>
</asp:TemplateField>
我在网格视图中也有一个radiobuttonlist,有一个正在运行的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中,所以它出现在每一行......
感谢任何帮助。
答案 0 :(得分:1)
就个人而言,我会为你的隐藏字段分配一个类,并尝试像这样访问它:
$(this).closest(".myClass");
请记住,由于这是一个服务器控件,因此该隐藏字段的ID很可能会附加一堆asp.net垃圾。所以不要像这样呈现:
<input type="hidden" value="0" id="hdnIsChanged" />
它很可能会像这样呈现:
<input type="hidden" value="0" id="clt100_clt100_290420349823049823423_hdnIsChanged" />
答案 1 :(得分:0)
尝试alert($("#hdnIsChanged").val())
答案 2 :(得分:0)
alert($('input[id$=hdnAnswered').val());
但现在试试这个......你忘了关闭方括号
alert($('input[id$=hdnAnswered]').val());
以下是可以帮助您获取特定行的隐藏字段值的代码
$("#MainContent_gvLineItems input[id*='rbAnswer']").click(function () {
var parentRow = $(this).parent('tr');
var hiddenField=parentRow.find('input[id$=hdnIsChanged]');
alert(hiddenField.val());
});