简体   繁体   English

使用jqueryui sortable从ul li与孩子进行序列化

[英]Serialize from ul li with child using jqueryui sortable

I would like to know how to create a JSON or serialize (both is fine) from a ul including 我想知道如何从ul中创建JSON或序列化(都很好)

<ul class="menu send ui-sortable">
<li id="pageid_1" class="ui-sortable-handle">Inscription
    <ul class="menu send ui-sortable">
         <li id="pageid_2" class="ui-sortable-handle">Joueurs en ligne
               <ul class="menu send ui-sortable"></ul>
         </li>
    </ul>
</li>

I cannot find how to create something like this: 我找不到如何创建这样的东西:

pageid[]=1&pageid[1]=2 OR [{"pageid":1,"children":[{"pageid":2}]}] pageid[]=1&pageid[1]=2[{"pageid":1,"children":[{"pageid":2}]}]

Meaning, including parent ID in [] . 含义,包括[]父ID。

Thank you for your help! 谢谢您的帮助!

This code will produce the output required 此代码将产生所需的输出

var result = [].map.call(document.querySelectorAll('ul.menu.send.ui-sortable li.ui-sortable-handle'), function(li) {
    var parent = '';
    if (li.parentNode && li.parentNode.parentNode && li.parentNode.parentNode.nodeName == 'LI' && li.parentNode.parentNode.matches('li.ui-sortable-handle')) {
        parent = li.parentNode.parentNode.id.replace(/\D+/g, '');
    }
    return "pageid[" + parent + "]=" + li.id.replace(/\D+/g, '');
}).join('&');
console.log(result); // pageid[]=1&pageid[1]=2

I haven't thought about how to do the second format, because the first format is easier to produce 我没有考虑过如何制作第二种格式,因为第一种格式更易于生成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM