admin管理员组

文章数量:1332389

First of all, I am not using the jQuery-UI, only latest jQuery!

I have following list:

<ul id="audio_list">
    <li id="trackid_1"></li>
    <li id="trackid_5"></li>
    <li id="trackid_2"></li>
    <li id="trackid_4"></li>
    <li id="trackid_3"></li>
</ul>

Now I'd like to serialize this list and save it as an array in a variable, like:

var myaudiotracks = jQuery('#audio_list').serialize();

All I get is an empty string, of course, because I am missing something or it is not possible to serialize lists with jQuery only.

What I am trying to acplish is to send this variable as an array to a PHP script, the result when posting the variable should be:

trackid[] 1
trackid[] 5
trackid[] 2
trackid[] 4
trackid[] 3

Any ideas how I can get to this result?

First of all, I am not using the jQuery-UI, only latest jQuery!

I have following list:

<ul id="audio_list">
    <li id="trackid_1"></li>
    <li id="trackid_5"></li>
    <li id="trackid_2"></li>
    <li id="trackid_4"></li>
    <li id="trackid_3"></li>
</ul>

Now I'd like to serialize this list and save it as an array in a variable, like:

var myaudiotracks = jQuery('#audio_list').serialize();

All I get is an empty string, of course, because I am missing something or it is not possible to serialize lists with jQuery only.

What I am trying to acplish is to send this variable as an array to a PHP script, the result when posting the variable should be:

trackid[] 1
trackid[] 5
trackid[] 2
trackid[] 4
trackid[] 3

Any ideas how I can get to this result?

Share asked Sep 11, 2013 at 2:42 lickmycodelickmycode 2,0793 gold badges19 silver badges20 bronze badges 1
  • 1 As per what i know the .serialize() works for the form elements having name-value pair. But not sure about this. – Ashis Kumar Commented Sep 11, 2013 at 2:47
Add a ment  | 

3 Answers 3

Reset to default 5

Try

var array = jQuery('#audio_list li').map(function(){
    return 'trackid[]=' + this.id.match(/(\d+)$/)[1]
}).get()
console.log(array.join('&'))

Demo: Fiddle

serialize() is to encode a set of form elements as a string for submission.

Try using .each() and loop all li elements inside div:

$(document).ready(function(){
    var myaudiotracks = new Array();
    jQuery('#audio_list li').each(function(){
        myaudiotracks.push($(this).attr("id").split("trackid_")[1]);
    });
    console.log(myaudiotracks);
});

DEMO FIDDLE

you can't use jQuery serialize function like this,it only works for the form elements,so if you want to get the result like that,try this:

<ul id="audio_list">
    <li id="trackid_1"><input type="hidden" name="trackid" value="1"></li>
    <li id="trackid_5"><input type="hidden" name="trackid" value="5"></li>
    <li id="trackid_2"><input type="hidden" name="trackid" value="2"></li>
    <li id="trackid_4"><input type="hidden" name="trackid" value="4"></li>
    <li id="trackid_3"><input type="hidden" name="trackid" value="3"></li>

var myaudiotracks = jQuery('#audio_list li').serialize();

本文标签: phpHow to serialize all liid within an ul with jqueryserialize()Stack Overflow