Get the order of list item in jquery sortable

后端 未结 5 601
后悔当初
后悔当初 2021-02-04 12:34

I have done this http://jsbin.com/UBezOVA/1/edit.

When I click the submit button, i want to get the current order of the list item. But, it seems that $(\"#sortab

5条回答
  •  一个人的身影
    2021-02-04 13:20

    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);
    }
    

    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:

    • Item 1
    • Item 2
    • Item 3
    • Item 4
    • Item 5

    Put those two together and it will work as you expect: http://jsbin.com/UBezOVA/6/edit

提交回复
热议问题