简体   繁体   English

使用内部的每个DIV构建div ID数组

[英]Build a array of div's id using each DIV inside section

I'm trying to get the ID of each DIV inside this HTML code 我正在尝试获取此HTML代码中每个DIV的ID

<section id="choices">    
    <div id="talla_choice_24" style="">
        ...
    </div>
    <div id="color_choice_25" style="">
        ...
    </div>
    <div id="sport_choice_26" style="">
        ...
    </div>

    <button type="button" class="create-variation" id="create-variation" style="">Crear variaciones</button>
    <section id="variations_holder" style="display: none"></section>
</section>

So I made this: 所以我做了这个:

function getDivId(div) {
    var inputValues = [];
    $("#" + div + ' > div').each(function() {
        inputValues.push($(this).val());
    })
    return inputValues;
}

And I call here: 我在这里打电话:

$('#choices').on("click", "#create-variation", function(e) {
    var parent_id = $(this).closest("section").attr("id");
    var element = getDivId(parent_id);

    iterateChoices("", element[0], element.slice(1), 0);
});

I need to build something like this: 我需要建立这样的东西:

var element = new Array($('#talla_choice_24 input:text'), $('#color_choice_25 input:text'), $('#sport_choice_26 input:text'));

But I get this error: 但是我得到这个错误:

Uncaught TypeError: Object has no method 'each' 未捕获的TypeError:对象没有方法“每个”

What is wrong? 怎么了?

UPDATE UPDATE

This is the code for iterateChoices() function: 这是iterateChoices()函数的代码:

function iterateChoices(row, element, choices, counter) {
    if ($.isArray(choices) && choices.length > 0) {
        element.each(function(index, item) {
            if (counter == 0)
                row = '<input type="text" required="required" value="" name="pupc[]" /><input type="text" required="required" value="" name="pprice[]" /><input type="text" required="required" value="" name="pqty[]" />';

            iterateChoices(row + '<input value="' + item.value + '">', choices[0], choices.slice(1), counter + 1);
        });
    } else {
        html_temp = "";
        $.each(element, function(index, item) {
            html_temp += row + '<input value="' + item.value + '"><br>';
        });
        html += html_temp;
    }
}

I also made some changes at this code: 我还对该代码进行了一些更改:

function getDivId(div) {
    var inputValues = [];
    $("#" + div + ' > div').each(function() {
        inputValues.push("#" + $(this).attr('id') + ' input:text');
    });

    return inputValues;
}

And now the error change to this: 现在错误更改为:

Uncaught TypeError: Object #talla_choice_24 input:text has no method 'each' 未捕获的TypeError:对象#talla_choice_24 input:text没有方法“每个”

UPDATE 2 更新2

I still continue change getDivId() function to build a array like this: 我仍然继续更改getDivId()函数来构建像这样的数组:

 var element = new Array($('#talla_choice_24 input:text'), $('#color_choice_25 input:text'), $('#number_choice_23 input:text'), $('#sport_choice_23 input:text'));

But can't get it since array values are constructed as strings, see below: 但由于数组值被构造为字符串,因此无法获取它,请参见下文:

function getDivId(div) {
    var inputValues = [];
    $("#" + div + ' > div').each(function() {

        inputValues.push('$("#' + $(this).attr('id') + ' input:text")');
    });

    return inputValues;
}

I'm getting: 我越来越:

("$('#talla_choice_24 input:text')", "$('#color_choice_25 input:text')")

I think there is the problem 我认为有问题

You are using the val method on a div element, which just returns an empty string. 您在div元素上使用val方法,该方法仅返回一个空字符串。 There is no each method on a string. 字符串上没有each方法。


You don't need the id of each div to get the input elements inside them. 您不需要每个div的id即可在其中获取输入元素。 This will get you the inputs in a single jQuery object: 这将为您提供单个jQuery对象中的输入:

var element = $(this).closest("section").find("> div input:text");

If you really need an array of separate jQuery objects, you can then do this: 如果您确实需要一组单独的jQuery对象,则可以执行以下操作:

element = element.map(function(){ return $(this); }).get();
var arr = [];
$("#create-variation").on('click', function(){
    $("#choices > div").each(function(a,b){
        arr.push($(this).text());
    });
});

After some headaches I realized how to fix the (my) error, here is the solution to all the problems: 经过一番头痛之后,我意识到了如何解决(我的)错误,这是所有问题的解决方案:

function getDivId(div) {
    var inputValues = [];
    $("#" + div + ' > div').each(function() {
        inputValues.push($("#" + $(this).attr('id') + " input:text"));
    });

    return inputValues;
}

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

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