简体   繁体   English

如何通过嵌套子列表中的JavaScript保存父ID

[英]How to save parent id through javascript in nested sublists

I have a nested list with any number of li's and any number of ul's inside the li's where every li has 2 hidden inputs: an id and an id of the parent. 我有一个嵌套列表,其中在li的内部有任意数量的li和ul,其中每个li都有2个隐藏的输入:一个id和一个父代的id。

<ul>
    <li>
        <input type="hidden" name="question[id][]" value="'. $sub['id'] .'" />
        <input type="hidden" name="question[parent][]" value="'. $question['id'] .'" />
        <ul>
            <li>
            <input type="hidden" name="question[id][]" value="'. $sub['id'] .'" />
            <input type="hidden" name="question[parent][]" value="'. $question['id'] .'" />
            </li>
        </ul>
    </li>
    <li>
        <input type="hidden" name="question[id][]" value="'. $sub['id'] .'" />
        <input type="hidden" name="question[parent][]" value="'. $question['id'] .'" />
        <ul>
            <li>
            <input type="hidden" name="question[id][]" value="'. $sub['id'] .'" />
            <input type="hidden" name="question[parent][]" value="'. $question['id'] .'" />
            </li>
        </ul>
        <ul>
            <li>
            <input type="hidden" name="question[id][]" value="'. $sub['id'] .'" />
            <input type="hidden" name="question[parent][]" value="'. $question['id'] .'" />
            </li>
        </ul>
    </li>

</ul>

I am using jQuery sortable to sort the nested list and now after sorting them I need to save the appropriate parent id to each item in the sublist. 我正在使用jQuery sortable对嵌套列表进行排序,现在对它们进行排序之后,我需要将适当的父ID保存到子列表中的每个项目。

Theoretically the parent id of each sub question is the value of the first input of the second closest ul. 从理论上讲,每个子问题的父ID是第二个最接近的ul的第一个输入的值。 Also if there is no parent ul, then I want to set the parent id to 0. How do I find it? 另外,如果没有父级ul,那么我要将父级id设置为0。如何找到它? This is what I have so far: 这是我到目前为止的内容:

$('#reorder_form').submit(function(){
    $('li').each(function() {
        $id = $(this).closest('ul').closest('li').find("input[name='question[id]']").val();
        if(typeof($id) == 'undefined') $id=0;
        $(this).find("input[name='question[parent]']").val($id);
    });
});

I am not 100% sure that this is how you properly loop through each input because I am hitting each twice. 我不是100%肯定这是您正确遍历每个输入的方式,因为我要敲两次。 Perhaps I should loop through the li's instead? 也许我应该遍历李氏?

You want to go up to the closest li, then up again to the closest li before that then back to the input. 您想要上升到最近的li,然后再上升到最近的li,然后返回到输入。 To be really semantic: 确实是语义上的:

$id = $(this).closest('li').closest('li').find('input[name^="question[id]"]').first().val();

Not checked. 未检查。 I would do it in jsfiddle but it appears to be broken. 我会用jsfiddle来做,但是好像坏了。

$('li input[name^="question[parent]"]').each(function() {
  var $id = $(this).closest('li').closest('li').find('input[name^="question[id]"]').val();
  if(typeof($id)=="undefined") $id=0; 
  $(this).val($id);
});

Just one note if .save points to a button in a form bear in mind there is (usually) more than one way to submit a form. 请记住,如果.save指向表单中的按钮,请注意(通常)提交表单的方法不只一种。 $('#theform').on('submit', function(){...}); is the best method. 是最好的方法。

Also

if(typeof($id) == undefined) 

I'm pretty sure that would always return false because typeof won't == undefined, it will be a value, which may be 'undefined'. 我很确定这将始终返回false,因为typeof不会== undefined,它将是一个值,可能是'undefined'。

if(typeof($id) == "undefined")

This is the code to save the id and parent id of any number of li's containing any number of ul's containing any number of li's with the cooresponding html from the question. 这是保存与问题对应的html的任何数量li的id和父代id的代码,其中li包含任意数量的ul,ul包含任意数量的li。 I admit it is slightly hackish, but it is what I came up with. 我承认这有点骇人听闻,但这是我想出的。 Hope it helps someone. 希望它可以帮助某人。

        $('#reorder_form').submit(function(){
            $('li input[name^="question[parent]"]').each(function() {
                $ul = $(this).closest('ul').parent().closest('ul');
                $attr = $ul.attr("class");
                if(typeof($attr) == 'undefined')
                    $id=0;
                else
                    $id = $ul.find("li input[name^='question[id]']").val();
                $(this).val($id);

            });
        });

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

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