C# 如何使用jquery获取或设置单选按钮列表的选定索引?
如何使用索引检查单选按钮。。 下面是我的asp.net C代码的Radio按钮列表C# 如何使用jquery获取或设置单选按钮列表的选定索引?,c#,jquery,asp.net,javascript-framework,C#,Jquery,Asp.net,Javascript Framework,如何使用索引检查单选按钮。。 下面是我的asp.net C代码的Radio按钮列表 <asp:RadioButtonList runat="server" ID="rdlCategory" CssClass="clsradio" AppendDataBoundItems="true" RepeatDirection="Horizontal" RepeatLayout="Flow" > </asp:RadioButtonList> 并使用C动态绑定它。。 Html看起来像
<asp:RadioButtonList runat="server" ID="rdlCategory" CssClass="clsradio" AppendDataBoundItems="true" RepeatDirection="Horizontal" RepeatLayout="Flow" >
</asp:RadioButtonList>
并使用C动态绑定它。。
Html看起来像
<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中使用selected 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);
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);
至于如何使用jQuery在ASP.NET中选择项目,我不确定它是否能在所有情况下都起作用。每次触摸ASP.NET控件时,ASP.NET都会更新ViewState,因此我不会使用太多jQuery来操作选择值
如果仍然需要此选项,则$selector.attrchecked为true
至于如何使用jQuery在ASP.NET中选择项目,我不确定它是否能在所有情况下都起作用。每次触摸ASP.NET控件时,ASP.NET都会更新ViewState,因此我不会使用太多jQuery来操作选择值
如果仍然需要此选项,则$selector.attrchecked为true 与选择框不同,单选按钮中实际上没有索引的概念 但是,您可以在jQuery中执行以下操作:
$(".clsradio input:checked").index();
如果只有一个组在.clsradio中,则将获取您的范围内所有选中的输入框,作为您使用的收音机,这些框应仅为一个
这是一个
编辑:
一个更全面但速度较慢的示例是:
$("input[name$='rdlCategory']:checked").index();
获取以rdlCategory结尾的输入。你甚至可以加上:收音机,但你不需要
编辑2-通过传入索引进行检查
您可以使用:
$(".clsradio input:nth-child(5)").attr("checked", "checked");
n使用输入选择器的子项可以用作索引。与选择框不同,单选按钮中实际上没有索引的概念 但是,您可以在jQuery中执行以下操作:
$(".clsradio input:checked").index();
如果只有一个组在.clsradio中,则将获取您的范围内所有选中的输入框,作为您使用的收音机,这些框应仅为一个
这是一个
编辑:
一个更全面但速度较慢的示例是:
$("input[name$='rdlCategory']:checked").index();
获取以rdlCategory结尾的输入。你甚至可以加上:收音机,但你不需要
编辑2-通过传入索引进行检查
您可以使用:
$(".clsradio input:nth-child(5)").attr("checked", "checked");
n使用输入选择器的子对象可以用作索引。未选中将给出未定义的,否则将返回所选对象的值
$('input[name=ctl00$cphTop$rdlCategory]:checked').val()
未选中将给出未定义,否则将返回选定对象的值
$('input[name=ctl00$cphTop$rdlCategory]:checked').val()
这对我有用:
$($("input[name='GroupName']").get(index)).prop('checked', true);
说明:
$input[name='GroupName']返回名为'GroupName'的项目列表
.getindex返回列表中的第i项。对于无线组,这将是您想要的输入标记
输入标记是HTML元素,而不是jQuery项,因此需要重新包装它。然后可以调用prop方法
这对我有用:
$($("input[name='GroupName']").get(index)).prop('checked', true);
说明:
$input[name='GroupName']返回名为'GroupName'的项目列表
.getindex返回列表中的第i项。对于无线组,这将是您想要的输入标记
输入标记是HTML元素,而不是jQuery项,因此需要重新包装它。然后可以调用prop方法
单选按钮不生成列表,它在流程布局中生成序列和标签。Hello@vivek。。如何传递名称而不是ctl00$cphTop$rdlCategory。。它变成动态的。。不固定。。当从页面源代码查看时,它是这样的…我不是asp.net开发人员…所以我不知道它的语法..但您只需传递包含radiobutton名称的变量..对于我的答案中的动态asp使用代码,其余的可以从Vivek获得。您可以使用$'input:radio'。attr'checked',true.indexinputindex;单选按钮不生成列表,它在流程布局中生成序列和标签。Hello@vivek。。如何传递名称而不是ctl00$cphTop$rdlCategory。。它变成动态的。。不固定。。当从页面源代码查看时,它是这样的…我不是asp.net开发人员…所以我不知道它的语法..但您只需传递包含radiobutton名称的变量..对于我的答案中的动态asp使用代码,其余的可以从Vivek获得。您可以使用$'input:radio'。attr'checked',true.indexinputindex;谢谢@Alex,我还想通过索引设置checked=true。。如果我通过了index=2,那么它应该检查category2抱歉@Abhishek我不明白如果我通过了index=2,那么它应该检查category2?你想自己在javascript中选择它们吗?事实上@@Abhishek别担心,我现在明白了-我会在几分钟后更新我的答案。如果没有人被选中,那么我想通过传递初始选择的整数值来选择。。是否有任何方法可以通过索引添加属性checked=true…@Abhishek尝试使用$'input:radio'.index0.attr'checked',true;谢谢@Alex,
我还想通过索引设置checked=true。。如果我通过了index=2,那么它应该检查category2抱歉@Abhishek我不明白如果我通过了index=2,那么它应该检查category2?你想自己在javascript中选择它们吗?事实上@@Abhishek别担心,我现在明白了-我会在几分钟后更新我的答案。如果没有人被选中,那么我想通过传递初始选择的整数值来选择。。是否有任何方法可以通过索引添加属性checked=true…@Abhishek尝试使用$'input:radio'.index0.attr'checked',true;