最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery: How to reverse sortable('serialize') arrays from last to first? - Stack Overflow

programmeradmin0浏览0评论

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

How to reverse it from last to first, updateList.php?id[]=5&id[]=4&id[]=3&id[]=2&id[]=1&&action=update?

<ul>
<li id="oreder-5">5</li>
<li id="oreder-4">4</li>
<li id="oreder-3">3</li>
<li id="oreder-2">2</li>
<li id="oreder-1">1</li>
<ul>

My code:

$(document).ready(function(){
    order=[];
    $('#list ul').children('li').each(function(idx, elm) { order.push(elm.id.split('-')[1]) });
    $.post('updateList.php', {'order[]': order, action: 'update'});

    function slideout(){
        setTimeout(function(){ $("#response").slideUp("slow", function () {}); }, 2000);
    }
    $("#response").hide();
 $(function() {
    $("#list ul").sortable({ opacity: 0.8, cursor: 'move', update: function() {
   var order = $(this).sortable("serialize") + '&action=update'; 
   $.post("updateList.php", order, function(theResponse){
    $("#response").html(theResponse);
    $("#response").slideDown('slow');
    slideout();
   });
    }});             
 });
});

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

How to reverse it from last to first, updateList.php?id[]=5&id[]=4&id[]=3&id[]=2&id[]=1&&action=update?

<ul>
<li id="oreder-5">5</li>
<li id="oreder-4">4</li>
<li id="oreder-3">3</li>
<li id="oreder-2">2</li>
<li id="oreder-1">1</li>
<ul>

My code:

$(document).ready(function(){
    order=[];
    $('#list ul').children('li').each(function(idx, elm) { order.push(elm.id.split('-')[1]) });
    $.post('updateList.php', {'order[]': order, action: 'update'});

    function slideout(){
        setTimeout(function(){ $("#response").slideUp("slow", function () {}); }, 2000);
    }
    $("#response").hide();
 $(function() {
    $("#list ul").sortable({ opacity: 0.8, cursor: 'move', update: function() {
   var order = $(this).sortable("serialize") + '&action=update'; 
   $.post("updateList.php", order, function(theResponse){
    $("#response").html(theResponse);
    $("#response").slideDown('slow');
    slideout();
   });
    }});             
 });
});
Share Improve this question edited May 23, 2017 at 12:17 CommunityBot 11 silver badge asked May 27, 2010 at 11:54 BinyaminBinyamin 7,80310 gold badges61 silver badges82 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

This will do it:

var reversed = $(this).sortable("serialize").split("&").reverse().join("&");
var order = reversed + '&action=update'; 

In other words:

  • Split the string at the '&'.
  • Reverse the resulting array.
  • Join the array with the '&' character to form the reversed serialized string.

Needed order.reverse(); before $.post(..);

发布评论

评论列表(0)

  1. 暂无评论