jQuery:如何从最后到第一个反转可排序('序列化')数组?

时间:2021-05-27 14:29:59

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

讨论开始jQuery:如何处理sortable('serialize')返回的列表?

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

如何将它从最后一个反转到第一个,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();
   });
    }});             
 });
});

2 个解决方案

#1


7  

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.
  • 使用'&'字符连接数组以形成反向序列化字符串。

#2


1  

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

需要order.reverse();在$ .post(..)之前;

#1


7  

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.
  • 使用'&'字符连接数组以形成反向序列化字符串。

#2


1  

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

需要order.reverse();在$ .post(..)之前;