Javascript 代码从不通过$。每个(元素,…)($。每个周期从不被调用)
我制定了以下代码:Javascript 代码从不通过$。每个(元素,…)($。每个周期从不被调用),javascript,jquery,Javascript,Jquery,我制定了以下代码: function iterateChoices(row, element, choices, counter) { if ($.isArray(choices) && choices.length > 0) { element.each(function(index, item) { if (counter === 0) { row = '<div style="dis
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);
}
这是每个console.log()的输出:
真的不知道它在哪里失败了你正在做的是$.inArray(选项)
,然后是$.each(元素)
。为什么?如果你console.log(行)
或console.log(html_temp)
在每个之后,你看到了什么?计数器的值是多少?你的第一个循环只有在计数器==0的情况下才能“工作”{.@Reynier在每个
中的第一行放置一个控制台.log
或警报
-在停止创建元素时可能会出现其他问题。只需先用一个简单的调试语句测试循环是否被输入即可。@Reynier可以设置吗?我可以看到它正在控制台中被记录,但我看不到您实际在哪里将结果输出到DOM?您没有返回任何内容?。。。
$.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">
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:
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