[英]Can't get the right $(this) element with onclick. Trying to use :not for input inside a div
[英]Javascript can't get an input element inside of a gridview
我在asp.net頁面上允許站點管理員選擇一個用戶作為“系統主席”。 為此,我在網格視圖中列出了用戶,並有一列單選按鈕來顯示當前主席是誰,或更改分配的主席。 我為按鈕使用了html輸入(type = radio)。 頁面加載后,如果為“系統”分配了“椅子”,則需要檢查相關的單選按鈕。 我正在嘗試使用javascript或jquery執行此操作。 我嘗試過的腳本是:
jQuery的嘗試1:
<script type="text/javascript">
function initRadioButtons() {
$("#user268").attr('checked', 'checked');
}
</script>
jQuery嘗試2:函數initRadioButtons(){$(“#user268”)。prop('checked',true); }
javascript(rb設置為null):
<script type="text/javascript">
function initRadioButtons() {
var rb = document.getElementById(user268");
}
</script>
這是我的.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>
我究竟做錯了什么?
我建議兩件事。 首先是JavaScript:
<script type="text/javascript">
$(document).ready(function(){
initRadioButtons();
});
function initRadioButtons() {
$(".user268").attr('checked', 'checked'); // notice i changed to class for next one
}
</script>
然后,我將檢查運行頁面的視圖源,它是否在ID字段中顯示了此內容?
id='user<%# Eval("userID") %>'
我認為您必須采取不同的做法。 必須運行runat="server"
才能進行數據綁定。 因為您有runat="server"
所以實際上您必須使用一個類,因為Webforms會自動分配一個ID。 它也必須是<%# %>
包含的完整屬性值。 所以我的猜測是這一行應該更像是:
<input runat="server" type="radio" class='<%# string.Format("user{0}", Eval("userID")) %>' name="isChair" value='<%# Eval("userID") %>' onclick="radioButtonClicked(this);" />
現在我們已經走到了遠了,我想您會遇到的下一個問題是您正在GridView中使用<input>
標記,因此它不會作為綁定的一部分發布回去。 通常,您將使用<asp:RadioButton>
來執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.