【问题标题】:jQuery: How to reverse sortable('serialize') arrays from last to first?jQuery:如何将可排序('serialize')数组从最后一个反转到第一个?
【发布时间】:2011-02-24 15:16:20
【问题描述】:

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

如何倒转从最后到第一,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>

我的代码:

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

【问题讨论】:

    标签: javascript jquery jquery-ui jquery-ui-sortable serialization


    【解决方案1】:

    这样就可以了:

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

    换句话说:

    • Split '&' 处的字符串。
    • Reverse 结果数组。
    • Join 带有 '&' 字符的数组组成逆序化字符串。

    【讨论】:

    • 谢谢@karim79!它适用于带有 cursor:'move' 效果的第二个 jquery 部分,但不适用于 order=[]; $('#list ul').children('li').each(function(idx, elm) { order.push(elm.id.split('-')[1]) }); $.post('updateList.php', {'order[]': order, action: 'update'});能否请您也为此寻求解决方案!?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2011-02-26
      • 1970-01-01
      • 2018-01-05
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      • 2021-10-05
      • 2018-08-29
      • 1970-01-01
      相关资源
      最近更新 更多