每个内部的Jquery都移动到父级

问题描述 投票:0回答:1

我不确定我是否正确地这样做但是我有这个内容列表需要注入插件生成的表单中。

我正在使用此代码来确定表单的每个部分,并将内容放在与输出匹配的列表中。

$('.vfb-page-section').each(function(){
    var sectionName = $('.vfb-page-title',this).text().toLowerCase();
    console.log(sectionName);
    // Get the section name and make it lowercase to match the div class name.

    $('.vfb-fieldType-radio',this).each(function(){
        var groupID = $(this).attr('id').replace('vfbField','');
        console.log(groupID);
        // Get the ID number of the group of inputs.

        $('input',this).each(function(){
            var inputID = $(this).attr('id').replace('vfb-field-'+groupID+'-','');
            console.log(inputID);
            // Get the ID of the input.

            var candidate = $('.cat-'+sectionName+' #'+inputID).clone();
            //Build the selector to find the correct div.
            console.log(candidate);
            $(this).parent().before(candidate);
            // The div needs to be appended to the parent of the input
        });
    });
});

页面上的div有这样的:

<div class="cat-sectionname" id="2">some content</div>

到目前为止,我已经设法让第一组将其内容转移到正确的输入父母。但似乎没有找到下一个groupID。

我错过了什么?

jquery loops each
1个回答
0
投票

您的候选人选择器正在寻找2个元素而不是1个具有给定类和ID的元素。删除空格,它应该找到元素:

var candidate = $('#'+inputID+'.cat-'+sectionName).clone();

空格意味着它在具有类的元素内查找具有id的元素。 Related answer here.

© www.soinside.com 2019 - 2024. All rights reserved.