繁体   English   中英

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

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

我正在尝试获取此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>

所以我做了这个:

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

我在这里打电话:

$('#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);
});

我需要建立这样的东西:

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

但是我得到这个错误:

未捕获的TypeError:对象没有方法“每个”

怎么了?

UPDATE

这是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;
    }
}

我还对该代码进行了一些更改:

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

    return inputValues;
}

现在错误更改为:

未捕获的TypeError:对象#talla_choice_24 input:text没有方法“每个”

更新2

我仍然继续更改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'));

但由于数组值被构造为字符串,因此无法获取它,请参见下文:

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

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

    return inputValues;
}

我越来越:

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

我认为有问题

您在div元素上使用val方法,该方法仅返回一个空字符串。 字符串上没有each方法。


您不需要每个div的id即可在其中获取输入元素。 这将为您提供单个jQuery对象中的输入:

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

如果您确实需要一组单独的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());
    });
});

经过一番头痛之后,我意识到了如何解决(我的)错误,这是所有问题的解决方案:

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