[英]How to get or set Selected index of the Radio Button List using jquery?
我如何使用索引檢查單選按鈕。以下是我的asp.net C#代碼,用於單選按鈕列表...
<asp:RadioButtonList runat="server" ID="rdlCategory" CssClass="clsradio" AppendDataBoundItems="true" RepeatDirection="Horizontal" RepeatLayout="Flow" >
</asp:RadioButtonList>
並使用C#動態綁定它。
<span class="clsradio" id="ctl00_cphTop_rdlCategory"><input type="radio" value="8" name="ctl00$cphTop$rdlCategory" id="ctl00_cphTop_rdlCategory_0">
<label for="ctl00_cphTop_rdlCategory_0">category1</label>
<input type="radio" value="11" name="ctl00$cphTop$rdlCategory" id="ctl00_cphTop_rdlCategory_1">
<label for="ctl00_cphTop_rdlCategory_1">category2</label>
<input type="radio" value="22" name="ctl00$cphTop$rdlCategory" id="ctl00_cphTop_rdlCategory_2">
<label for="ctl00_cphTop_rdlCategory_2">category3</label>
<input type="radio" value="33" name="ctl00$cphTop$rdlCategory" id="ctl00_cphTop_rdlCategory_3">
<label for="ctl00_cphTop_rdlCategory_3">category4</label>
<input type="radio" value="34" name="ctl00$cphTop$rdlCategory" id="ctl00_cphTop_rdlCategory_4">
<label for="ctl00_cphTop_rdlCategory_4">category5</label>
</span>
我想通過索引值將屬性checked = true添加到單選按鈕。
假設我通過了index = 2,那么應該選擇Category2 ..
或者也想在jquery中使用以選擇(選中= true)單選按鈕索引。
我該怎么做?
與選擇框不同,單選按鈕中實際上沒有索引的概念。
但是,您可以在jQuery中執行以下操作:
$(".clsradio input:checked").index();
這將選中您跨度內的所有輸入框(如果在.clsradio中只有一組,則使用廣播的輸入框應僅為一個)。
編輯:
一個更完整的證明示例(但較慢)是:
$("input[name$='rdlCategory']:checked").index();
它獲取以rdlCategory結尾的輸入。 您甚至可以添加“:radio”,但不必這樣做。
編輯2-通過傳入索引進行檢查。
您可以使用:
$(".clsradio input:nth-child(5)").attr("checked", "checked");
使用“輸入”選擇器的nth-child可用作索引。
這為我工作:
$($("input[name='GroupName']").get(index)).prop('checked', true);
說明:
$("input[name='GroupName']")
返回$("input[name='GroupName']")
的項目列表
.get(index)
返回列表中的第i
個項目。 對於無線電組,這將是您想要的輸入標簽。
輸入標簽是HTML元素,而不是jQuery項,因此您需要重新包裝它。 然后,您可以調用prop
方法。
$(html).prop('checked',true)
使用jQuery的索引方法
function selectRadioButton(inputindex){
$('input[name="ctl00$cphTop$rdlCategory"]').index(inputindex).attr('checked', true);
}
獲取選定的單選按鈕索引...
var selectedIndex = $('input[name="ctl00$cphTop$rdlCategory"]').attr('checked', true).index(inputindex);
$("#<%= rdlCategory.ClientID %> input:checked").index();
至於如何使用jQuery在ASP.NET中選擇項目,我不確定它是否在所有情況下都能正常工作。 每次您觸摸ASP.NET控件時,ASP.NET都會更新ViewState,因此我不會使用太多的jQuery來操縱選擇值
如果仍然需要,則$(selector).attr("checked",true);
未選中將給出“未定義”,否則將返回所選對象的值。
$('input[name=ctl00$cphTop$rdlCategory]:checked').val()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.