0

我做了这个代码:

function iterateChoices(row, element, choices, counter) {
    if ($.isArray(choices) && choices.length > 0) {
        element.each(function(index, item) {
            if (counter === 0) {
                row = '<div style="display:inline-block"><label style="display: inline-block">UPC:</label> <input style="display: inline-block" type="text" value="" name="pupc[]" /></div>';
                row += '<div style="display:inline-block"><label style="display: inline-block">Precio:</label> <input style="display: inline-block" type="text" value="" name="pprice[]" /></div>';
                row += '<div style="display:inline-block"><label style="display: inline-block">Cantidad:</label><input type="text" style="display: inline-block" value="" name="pqty[]" /></div>';
            }

            iterateChoices(row + '<div style="display:inline-block"><input style="display: inline-block" disabled="disabled" value="' + item.value + '"></div>', choices[0], choices.slice(1), counter + 1);
        });
        console.log("Counter: " + counter);
        console.log("Row: " + row);
    } else {
        html_temp = "";
        element.each(function(index, item) {
            html_temp += row + '<div style="display:inline-block"><input  style="display: inline-block" value="' + item.value + '" disabled="disabled"></div><br>';
        });
        html += html_temp;
    }
    console.log("html_temp: " + html_temp);
}

我测试了element作为值的内容:

$.isArray(element)
false

element.length
2

element
[
<input type=​"text" name=​"input_color[]​" class=​"field_color" data-id=​"color_5" placeholder=​"Color">​
, 
<input type=​"text" name=​"input_color[]​" class=​"field_color" data-id=​"color_5" placeholder=​"Color">​
]

$.each(element, function(index, item) { console.log(item) })
<input type=​"text" name=​"input_color[]​" class=​"field_color" data-id=​"color_5" placeholder=​"Color">​
<input type=​"text" name=​"input_color[]​" class=​"field_color" data-id=​"color_5" placeholder=​"Color">​

这是每个的输出console.log()

html_temp: 
Counter: 1
Row: <div style="display:inline-block"><label style="display: inline-block">UPC:</label> <input style="display: inline-block" type="text" value="" name="pupc[]" /></div><div style="display:inline-block"><label style="display: inline-block">Precio:</label> <input style="display: inline-block" type="text" value="" name="pprice[]" /></div><div style="display:inline-block"><label style="display: inline-block">Cantidad:</label><input type="text" style="display: inline-block" value="" name="pqty[]" /></div><div style="display:inline-block"><input style="display: inline-block" disabled="disabled" value="Red"></div>
html_temp: 
html_temp: 
Counter: 1
Row: <div style="display:inline-block"><label style="display: inline-block">UPC:</label> <input style="display: inline-block" type="text" value="" name="pupc[]" /></div><div style="display:inline-block"><label style="display: inline-block">Precio:</label> <input style="display: inline-block" type="text" value="" name="pprice[]" /></div><div style="display:inline-block"><label style="display: inline-block">Cantidad:</label><input type="text" style="display: inline-block" value="" name="pqty[]" /></div><div style="display:inline-block"><input style="display: inline-block" disabled="disabled" value="Blue"></div>
html_temp: 
Counter: 0
Row: <div style="display:inline-block"><label style="display: inline-block">UPC:</label> <input style="display: inline-block" type="text" value="" name="pupc[]" /></div><div style="display:inline-block"><label style="display: inline-block">Precio:</label> <input style="display: inline-block" type="text" value="" name="pprice[]" /></div><div style="display:inline-block"><label style="display: inline-block">Cantidad:</label><input type="text" style="display: inline-block" value="" name="pqty[]" /></div>
html_temp:

由于某种原因,在实际代码中它永远不会出现低谷$.each(element, ...),我找不到错误在哪里,有什么帮助或建议吗?

更新 我为测试目的创建了这个小提琴,它是如何工作的:

  1. 标记任何复选框
  2. 为每组选项添加任意数量的输入(颜色、塔拉)
  3. 单击“创建变体”按钮

行为:如果你选中最新的复选框,事情就按我的意愿工作,如果你没有,那么代码停止并且永远不会得到我想要的结果

更新 2:

console.log()在调用函数时添加了一些语句并查看结果:

function iterateChoices(row, element, choices, counter) {
    console.log("element: " + element);
    console.log("choices: " + choices);
    console.log("counter: " + counter);
    ...

element:  
[input.field_color, input.field_color, prevObject: x.fn.x.init[1], context: document, selector: "#color_choice_5 input:text", jquery: "1.10.2", constructor: function…]
choices:  
[x.fn.x.init[1], x.fn.x.init[0]]
counter:  0
element:  
[input.field_talla, prevObject: x.fn.x.init[1], context: document, selector: "#talla_choice_6 input:text", jquery: "1.10.2", constructor: function…]
choices:  
[x.fn.x.init[0]]
counter:  1
element:  
[prevObject: x.fn.x.init[1], context: document, selector: "#talla_choice_13 input:text", jquery: "1.10.2", constructor: function…]
choices:  []
counter:  2
0
element:  
[input.field_talla, prevObject: x.fn.x.init[1], context: document, selector: "#talla_choice_6 input:text", jquery: "1.10.2", constructor: function…]
choices:  
[x.fn.x.init[0]]
counter:  1
element:  
[prevObject: x.fn.x.init[1], context: document, selector: "#talla_choice_13 input:text", jquery: "1.10.2", constructor: function…]
choices:  []
counter:  2
0

真的不知道哪里出错了

4

0 回答 0