jQuery: What to do with the list that sortable('serialize') returns?

前端 未结 8 1504
我寻月下人不归
我寻月下人不归 2020-12-23 20:01

With jQuery I\'m retrieving positions of a sortable list using \'serialize\', like this:

var order = $(\'ul\').sortable(\'serialize\');

The vari

8条回答
  •  抹茶落季
    2020-12-23 20:14

    I think the best way is not to use sortable('serialize') at all, but use jQuery to simply iterate over the sorted ids, like so:

    order = [];
    $('ul').children('li').each(function(idx, elm) {
      order.push(elm.id.split('_')[1])
    });                                     
    $.get('ajax.php', {action: 'updateOrder', 'order[]': order});
    

    This has an advantage over explicitly appending the serialized sortable to the URL (or a parameter) in that it doesn't include the order[] parameter at all if there are no li in the list. (When I had this problem I was using sortable(connectWith: 'ul') so it was entirely possible for a user to drag all the li from one list). In contrast appending the serialized sortable would leave an unwanted '&'.

提交回复
热议问题