简体   繁体   English

在jquery-ui按钮集中获取第n个按钮

[英]Get nth button in a jquery-ui buttonset

Is there some way to get the nth button in a jquery-ui buttonset? 有什么方法可以在jquery-ui按钮集中获取第n个按钮吗?

 $( "#radio" ).buttonset(); 
 <link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <div id="radio"> <input type="radio" id="sizzle" name="project"> <label for="sizzle">Sizzle</label> <input type="radio" id="qunit" name="project" checked="checked"> <label for="qunit">QUnit</label> <input type="radio" id="color" name="project"> <label for="color">Color</label> </div> 

I can select button qunit using its id selector. 我可以使用其id选择器选择按钮qunit。

But is there any way to select say the 2nd button in this set? 但是,有什么方法可以选择说这组中的第二个按钮吗?

There are multiple ways you can select it in jQuery 您可以通过多种方式在jQuery中选择它

$("#radio input[type=radio]").eq(1)

OR 要么

$("#radio input[type=radio]").get(1)

OR 要么

$("#radio input[type=radio]:eq(1)")

You can alway use jquery selectors like :eq : 您可以始终使用:eq的 jquery选择器:

 $("#radio :radio:eq(1)"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <div id="radio"> <input type="radio" id="sizzle" name="project"> <label for="sizzle">Sizzle</label> <input type="radio" id="qunit" name="project" checked="checked"> <label for="qunit">QUnit</label> <input type="radio" id="color" name="project"> <label for="color">Color</label> </div> 

Alternative: 选择:

$("#radio :radio:nth-child(1)");
var n = 2;
var nthButton = $( "#radio input[type='radio']" ).buttonset().eq(n);

Adding input[type='radio'] selects the radio buttons themselves instead of the container div, eq(n) selects only the nth button. 添加input[type='radio']选择单选按钮本身而不是容器div, eq(n)仅选择第n个按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM