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
3 Answers
Reset to default 5Try
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
版权声明:本文标题:php - How to serialize all li#id within an ul with jquery.serialize()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742290101a2447635.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论