如何使用jQuery序列化多个列表 [英] Howto serialize multiple Lists with Jquery

查看:68
本文介绍了如何使用jQuery序列化多个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个可排序的UL和一个简单的jquery/javascript

I have 3 sortable UL's and a simple jquery/javascript

<ul class="sortable" id="menu1">
  <li id="id_1">whatever</li>
  <li id="id_2">you</li>
</ul>
<ul class="sortable" id="menu2">
  <li id="id_3">wanne</li>
  <li id="id_4">put</li>
</ul>
<ul class="sortable" id="menu3">
  <li id="id_5">in</li>
  <li id="id_6">here</li>
</ul>

$(function() {
    $("ul.sortable").sortable({
        connectWith: 'ul'
        });
    });
</script>

LI在UL之间可拖动 我该如何序列化它,以便得到例如menu1 [0] = 1& menu1 [1] = 3 还是和array或我可以用来保存状态的任何东西?

LI's are draggable between UL's How can i serialize this so i get for example menu1[0]=1&menu1[1]=3 Or and array or whatever i can work with to save the state?

推荐答案

更新

演示: http://jsbin.com/evoru4/2

在您的情况下使用此:

$(function() {
    $("ul.sortable").sortable({
        connectWith: '.sortable',
        update: function(event, ui) {
        var position = $('.sortable').serial();
        alert( position );
        }
    });
});

//此功能使您的UL LI成为序列化对象

// this function make your UL LI a serialized object

(function($) {
    $.fn.serial = function() {
        var array = [];
        var $elem = $(this);
        $elem.each(function(i) {
            var menu = this.id;
            $('li', this).each(function(e) {
                array.push(menu + '[' + e + ']=' + this.id);
            });
        });
        return array.join('&');
    }
})(jQuery);

给你这样的东西:

menu1[0]=id_1&menu1[1]=id_2&menu2[0]=id_4&menu2[1]=id_6&menu3[0]=id_5&menu3[1]=id_3

代表每个元素的位置顺序

that is representing the position order of each element

这篇关于如何使用jQuery序列化多个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆