简体   繁体   English

如何更改jQuery选择器返回的元素(单选按钮)的顺序?

[英]How to change the order of elements (radio buttons) returned by jQuery selector?

I have a table with radio buttons. 我有一张带单选按钮的桌子。

In a jQuery collection, as c = $('input[name=mybuttons]') , I get them by rows, so that c[0] is the one in the top left cell. 在jQuery集合中,由于c = $('input[name=mybuttons]') ,我按行获取它们,因此c[0]是左上单元格中的那个。

How do I change this order, so that, say, c[0] be some another cell, or arrow keys go, say, by columns and not by rows; 如何更改此顺序,例如c[0]是另一个单元格,或者箭头键例如按列而不是按行; so that tab brings to some specific button rather than top left, and so that right arrow traverse the buttons in some specific order and not left-to-right? 以便该标签带到某个特定的按钮而不是左上角,从而使右箭头以某种特定的顺序而不是从左到右遍历这些按钮?

For checkboxes and tab key, I achieve this with tabindex . 对于复选框和Tab键,我可以通过tabindex实现。 How to achieve a similar effect for radio buttons and the right key: specify in HTML in what order the right key should traverse them? 如何使单选按钮和右键具有相似的效果:以HTML格式指定右键应以哪种顺序遍历它们?

Fiddle: http://jsfiddle.net/2u30u7fp/3/ The tab key does iterate in the desired order, but the right and left keys don't. 小提琴: http : //jsfiddle.net/2u30u7fp/3/ Tab键会按所需顺序进行迭代,但左右键则不会。

您可以使用下划线进行此操作,因为一旦获得c ,您基本上就拥有一个元素列表。

var sortedC = _.sortBy(c, function (elem) { return elem.propertyToSort; });

Here you are: use .insertBefore() or .insertAfter() . 在这里,您是:使用.insertBefore().insertAfter() In my example, if you choose a radio button, that button will go to first in the group. 在我的示例中,如果选择单选按钮,则该按钮将转到组中的第一个按钮。

 $(document).on("change", "input[name='sex']", function() { var index = $("input[name='sex']").index($(this)); console.log(index); if (index != 0) { $(this).parent('div').insertBefore("div:first"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <input type="radio" name="sex" value="male">Male</div> <div> <input type="radio" name="sex" value="female">Female</div> 

Hope this helps. 希望这可以帮助。

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

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