使用jQuery序列化div中的所有ID [英] Serialize all id's in a div using jquery
问题描述
我正在尝试在CMS上创建页面拖放功能的重新排序.由于某些页面具有子页面,因此我们在列表中嵌套了很多列表.代码示例:
I am trying to create a re-ordering of the page drag and drop feature on my CMS. As some of the pages have sub pages, we have a lot of lists nested within lists. An example of the code:
$(document).ready(function(){
$(function() {
$('#contentLeft ul').sortable({
update: function() {
var order = $('#contentLeft ul').sortable('serialize');
alert(order);
}
});
});
});
<div id="contentLeft">
<ul id="sitemap">
<li id="page_1" class="page_container">
Test
<ul>
<li id="page_20">Nested one</li>
<li id="page_30">nested 2</li>
<li id="page_40"> Nested 6</li>
</ul>
</li>
<li id="page_4" class="page_container">
Test
</li>
<li id="page_5" class="page_container">
Test
</li>
</ul>
</div>
在上面的示例中,当我四处移动并序列化ul时,我得到了返回的顶级列表元素的列表(page [] = 1,page [] = 4,page [] = 5).我需要的是将所有li标签(包括子li标签)序列化,并得到类似(page [] = 1,page [] = 20,page [] = 30,page [] = 40,page [ ] = 4,第[] = 5页).
In the above example, when I move things around and serialize the ul, i get the list of top level lists elements returned (page[]=1, page[]=4, page[]=5). What I need is for it to serialize ALL of the li tags, including the children li tags and get something like (page[]=1, page[]=20, page[]=30, page[]=40, page[]=4, page[]=5).
我试着很简单地将序列化应用于
I have tried applying serialize quite simply to
但没有运气.
有人能指出我正确的方向吗?
Could anyone point me in the right direcition?
非常感谢
推荐答案
$(function() {
$('#contentLeft ul').sortable({
update: function() {
var order3 = [];
$('#contentLeft ul li').each(function(){
order3.push($(this).attr('id').replace(/_/g, '[]='))
});
alert(order3.join('&'));
}
});
});
在这里尝试: http://jsfiddle.net/MXCZx/1/
这篇关于使用jQuery序列化div中的所有ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!