簡體   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