Serialize all id's in a div using jquery

别来无恙 提交于 2019-12-06 23:41:01
$(function() {
    $('#contentLeft ul').sortable({
        update: function() {
            var order3 = [];
            $('#contentLeft ul li').each(function(){
                order3.push($(this).attr('id').replace(/_/g, '[]='))
            });
            alert(order3.join('&'));
        }
    });
});

try here: http://jsfiddle.net/MXCZx/1/

Alternatively you could do like this:

// serialize input elements within a specific #id
$('#id :input').serialize();


// serialize a specific element in the form
$('input[name=inputName]').serialize();

have made for my self few days ago:

function serializeUL(ul){
    var children = {};
    $(ul).children().each(function(){
        var li = $(this),
            sub = li.children('ul');
        children[this.id] = sub.length > 0  ? serializeUL(sub) : null;
    })
    return children;
}

serializeUL('#sitemap') //will return you following JSON

{
   "page_1": {
        "page_20":null,
        "page_30":null,
        "page_40":null
   },
   "page_4":null,
   "page_5":null
} 

bit extended example of use:

http://jsfiddle.net/acrashik/E2Vte/8/

Use without form.

$(document).ready(function () {
//select html element
    $('li').click(function () {
        $(this).toggleClass('selected');
        if ($('li.selected').length == 0)
            $('.select').removeClass('selected');
        else
            $('.select').addClass('selected');
        counter();
    });
//ajax send id html element    
    $(document).on('click', 'li', function () {
        $(this).toggleClass("check");
        var form = document.querySelectorAll('li.selected'); //select the need classes
        var id = {};//empty array
        for (var i = 0; i < form.length; i++) {
            id[i] = form[i].id; //fill the array
        }
        $.ajax({
            url: "/site/search",
            type: 'post',
            dataType: "json",
            data: {"selected": id},
            success: function(data, response, textStatus, jqXHR) {
                console.log(data);
            }
        });
        return false;
    });
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!