获取订单位置 JQuery 可排序列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4163779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the order position JQuery sortable list?
提问by pegasus
How can i get the order position of Jquery sortable list?
如何获取 Jquery 可排序列表的顺序位置?
回答by Nick Craver
If your elements have IDs (if they don't, pick a prefix and number them) you can use the toArray
methodto get the array of IDs in their current order, for example:
如果您的元素有 ID(如果没有,请选择前缀并为其编号),您可以使用该toArray
方法以当前顺序获取 ID 数组,例如:
var idsInOrder = $(selector).sortable("toArray");
If you want to find the position of a particular one, use $.inArray()
, like this:
如果要查找特定位置的位置,请使用$.inArray()
,如下所示:
var index = $.inArray("idToLookFor", idsInOrder);
This will return the 0
-based index in the list the ID you're looking for is, or -1
if it's not found.
这将返回0
列表中您要查找的 ID 所在的基于索引的索引,或者-1
如果未找到它。
More generally, if you just want to get the index of an element amongst its siblings, use .index()
with no parameters, like this:
更一般地,如果您只想获取元素在其兄弟元素中的索引,请使用.index()
不带参数的方法,如下所示:
$("#myElem").index();
If your sortable was for example a <ul>
with sortable <li>
children, calling this on an <li>
would also get it's 0
-based index, within it's siblings.
例如,如果您的 sortable 是<ul>
带有可排序<li>
孩子的 a,则在 an 上调用<li>
它也会0
在它的兄弟姐妹中获得基于它的索引。