获取可排序的jQuery中列表项的顺序 [英] Get the order of list item in jquery sortable
问题描述
我已经完成了 http://jsbin.com/UBezOVA/1/edit .
当我单击提交按钮时,我想获取列表项的当前顺序.但是,看来
$("#sortable2").sortable("toArray")
不显示列表的当前顺序(例如sortable2).
如何获取列表的当前顺序.
When I click the submit button, i want to get the current order of the list item. But, it seems that
$("#sortable2").sortable("toArray")
does not show the current order of list (for example sortable2).
How to get the current order of a list.
推荐答案
问题的一部分是印刷错误,从jQuery选择器的ID中省略了#
.否则,您对.sortable("toArray")
的用法是正确的. (请注意,我在那里使用的是console.log()
而不是alert()
-注意浏览器的控制台以获得更好的输出)
Part of your issue is a typographical error, omitting the #
from the id in your jQuery selector. Otherwise, your usage of .sortable("toArray")
is correct. (Note, I used console.log()
there instead of alert()
- watch the browser's console for better output)
function submit(){
var idsInOrder = $("#sortable2").sortable("toArray");
//-----------------^^^^
console.log(idsInOrder);
}
但是,已记录,toArray()
方法将通过以下方式使用可排序项的id
属性:序列化时默认.您需要为每个可排序项添加一个唯一的id
属性,此功能才能起作用:
However, as documented the toArray()
method will use the id
attribute of sortable items by default when serializing. You will need to add a unique id
attribute to each of the sortable items for this to work:
<ul id="sortable2" class="connectedSortable">
<li class="ui-state-highlight" id='i1'>Item 1</li>
<li class="ui-state-highlight" id='i2'>Item 2</li>
<li class="ui-state-highlight" id='i3'>Item 3</li>
<li class="ui-state-highlight" id='i4'>Item 4</li>
<li class="ui-state-highlight" id='i5'>Item 5</li>
</ul>
将这两者放在一起,它将按您期望的那样工作: http://jsbin.com/UBezOVA/6/edit
Put those two together and it will work as you expect: http://jsbin.com/UBezOVA/6/edit
这篇关于获取可排序的jQuery中列表项的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!