[英]How to get or set Selected index of the Radio Button List using jquery?
How I can radio button checked using index.. Below is my asp.net C# code for radion button list... 我如何使用索引检查单选按钮。以下是我的asp.net C#代码,用于单选按钮列表...
<asp:RadioButtonList runat="server" ID="rdlCategory" CssClass="clsradio" AppendDataBoundItems="true" RepeatDirection="Horizontal" RepeatLayout="Flow" >
</asp:RadioButtonList>
and Bind it using C# dynamically.. and Html looks like 并使用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>
I want to add attribute checked=true to radio button by index value . 我想通过索引值将属性checked = true添加到单选按钮。
suppose, I passed index=2 then it should be selected Category2 .. 假设我通过了index = 2,那么应该选择Category2 ..
or also want to get selected (checked=true) radio button index using in jquery.. 或者也想在jquery中使用以选择(选中= true)单选按钮索引。
How I do this? 我该怎么做?
Unlike select boxes, there isn't really a concept of an index in radio buttons. 与选择框不同,单选按钮中实际上没有索引的概念。
However you can do something like this in jQuery: 但是,您可以在jQuery中执行以下操作:
$(".clsradio input:checked").index();
Which will get all the input boxes within your span that are checked (which as your using radio's should only be one, if only one group is inside .clsradio). 这将选中您跨度内的所有输入框(如果在.clsradio中只有一组,则使用广播的输入框应仅为一个)。
Here is an example on jsfiddle 这是jsfiddle上的示例
EDIT: 编辑:
A more full proof example (but slower) would be: 一个更完整的证明示例(但较慢)是:
$("input[name$='rdlCategory']:checked").index();
Which gets inputs ending in rdlCategory. 它获取以rdlCategory结尾的输入。 You could even add ":radio" but you don't need to.
您甚至可以添加“:radio”,但不必这样做。
EDIT 2 - Checking by passing in index. 编辑2-通过传入索引进行检查。
You can use: 您可以使用:
$(".clsradio input:nth-child(5)").attr("checked", "checked");
nth-child using the "input" selector can be used as the index. 使用“输入”选择器的nth-child可用作索引。
This is working for me: 这为我工作:
$($("input[name='GroupName']").get(index)).prop('checked', true);
Explanation: 说明:
$("input[name='GroupName']")
returns a list of items that have the name 'GroupName' $("input[name='GroupName']")
返回$("input[name='GroupName']")
的项目列表
.get(index)
returns the i
th item in the list. .get(index)
返回列表中的第i
个项目。 In the case of a radio group this would be the input tag you want. 对于无线电组,这将是您想要的输入标签。
The input tag is an HTML element, not a jQuery item, so you re-wrap it. 输入标签是HTML元素,而不是jQuery项,因此您需要重新包装它。 Then you can call the
prop
method. 然后,您可以调用
prop
方法。
$(html).prop('checked',true)
use index method of jquery 使用jQuery的索引方法
function selectRadioButton(inputindex){
$('input[name="ctl00$cphTop$rdlCategory"]').index(inputindex).attr('checked', true);
}
to get selected radio button index... 获取选定的单选按钮索引...
var selectedIndex = $('input[name="ctl00$cphTop$rdlCategory"]').attr('checked', true).index(inputindex);
$("#<%= rdlCategory.ClientID %> input:checked").index();
As for how select items in ASP.NET using jQuery, i'm not sure it will work in all scenarios. 至于如何使用jQuery在ASP.NET中选择项目,我不确定它是否在所有情况下都能正常工作。 ASP.NET updates ViewState each time you touch ASP.NET control thus i wouldn't use much jQuery to manipulate values of selection
每次您触摸ASP.NET控件时,ASP.NET都会更新ViewState,因此我不会使用太多的jQuery来操纵选择值
if you still want this then $(selector).attr("checked",true);
如果仍然需要,则
$(selector).attr("checked",true);
未选中将给出“未定义”,否则将返回所选对象的值。
$('input[name=ctl00$cphTop$rdlCategory]:checked').val()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.