[英]Javascript can't get an input element inside of a gridview
I an asp.net page that lets a site administrator select a user to be a 'systems chair'. 我在asp.net页面上允许站点管理员选择一个用户作为“系统主席”。 To do this, I list the users in a gridview, and have a column of radio buttons to show who the current chair is, or to change the assigned chair. 为此,我在网格视图中列出了用户,并有一列单选按钮来显示当前主席是谁,或更改分配的主席。 I'm using an html input (type=radio) for the buttons. 我为按钮使用了html输入(type = radio)。 When the page loads, if the 'system' has a 'chair' assigned to it, I need to check the related radiobutton. 页面加载后,如果为“系统”分配了“椅子”,则需要检查相关的单选按钮。 I'm trying to do this with javascript or jquery. 我正在尝试使用javascript或jquery执行此操作。 The scripts I've tried are: 我尝试过的脚本是:
jquery try 1: jQuery的尝试1:
<script type="text/javascript">
function initRadioButtons() {
$("#user268").attr('checked', 'checked');
}
</script>
jQuery try 2: function initRadioButtons() { $("#user268").prop('checked', true); jQuery尝试2:函数initRadioButtons(){$(“#user268”)。prop('checked',true); } }
javascript (rb is set to null): javascript(rb设置为null):
<script type="text/javascript">
function initRadioButtons() {
var rb = document.getElementById(user268");
}
</script>
Here is the gridview from my .aspx file: 这是我的.aspx文件中的gridview:
<Custom:GridView ID="gvUsers" DataSourceID="dsUsers" CellPadding="3" AutoGenerateColumns="false"
ArrowUpImageUrl="~/images/SortUpArrow.jpg" ArrowDownImageUrl="~/images/SortDownArrow.jpg"
Width="100%" ShowWhenEmpty="true" EmptyDataText="No Users Returned" DataKeyNames="userID"
AllowSorting="true" runat="server">
<Columns>
<asp:TemplateField HeaderText="AST Chair" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"
SortExpression="IsChair">
<ItemTemplate>
<input type="radio" id='user<%# Eval("userID") %>' name="isChair" value='<%# Eval("userID") %>' onclick="radioButtonClicked(this);" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Username" DataField="userName" sortExpression="userName" ItemStyle-HorizontalAlign="Left" />
<asp:BoundField HeaderText="First Name" DataField="FirstName" SortExpression="FirstName" ItemStyle-HorizontalAlign="Left" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" SortExpression="LastName" ItemStyle-HorizontalAlign="Left" />
</Columns>
</Custom:GridView>
What am I doing wrong? 我究竟做错了什么?
I'd suggest two things. 我建议两件事。 First the JavaScript: 首先是JavaScript:
<script type="text/javascript">
$(document).ready(function(){
initRadioButtons();
});
function initRadioButtons() {
$(".user268").attr('checked', 'checked'); // notice i changed to class for next one
}
</script>
Then I would check the view source of the running page, is it showing this in the ID field? 然后,我将检查运行页面的视图源,它是否在ID字段中显示了此内容?
id='user<%# Eval("userID") %>'
I think you have to do that line differently. 我认为您必须采取不同的做法。 It has to be runat="server"
for data binding to work. 必须运行runat="server"
才能进行数据绑定。 Because you have runat="server"
then you actually have to use a class because webforms will auto assign an ID. 因为您有runat="server"
所以实际上您必须使用一个类,因为Webforms会自动分配一个ID。 It also has to be the full attribute value contained in the <%# %>
. 它也必须是<%# %>
包含的完整属性值。 So my guess is this line should be more like: 所以我的猜测是这一行应该更像是:
<input runat="server" type="radio" class='<%# string.Format("user{0}", Eval("userID")) %>' name="isChair" value='<%# Eval("userID") %>' onclick="radioButtonClicked(this);" />
Now we have got this far I think the next problem you will have is that you are using an <input>
tag in a GridView so it wont be posted back as part of the binding. 现在我们已经走到了远了,我想您会遇到的下一个问题是您正在GridView中使用<input>
标记,因此它不会作为绑定的一部分发布回去。 Normally you would use <asp:RadioButton>
to do this. 通常,您将使用<asp:RadioButton>
来执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.