问题描述
我有以下表单,如果用户点击链接添加更多项目,我会动态添加更多下拉菜单(与这些相同)
I have the following form, and I am dynamically adding more dropdowns (exactly the same as these) if the user clicks on the link to add more items
<div class="dynamic-sale">
<select name="sizes[]" id="sizes" class="entry-dropdown">
<option value="3XL">3XL</option>
<option value="2XL">2XL</option>
<option value="XL">XL</option>
<option value="L">L</option>
<option value="M">M</option>
<option value="YL">YL</option>
<option value="YM">YM</option>
</select>
<select name="number[]" id="amount" class="entry-dropdown">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<select name="price[]" id="price" class="entry-dropdown">
<option value="15">$15</option>
<option value="12.5">$12.5</option>
<option value="10">$10</option>
<option value="0">Free</option>
</select>
</div>
我的问题是,我想拍摄结果并以我可以采用的格式提供它们添加所有内容。
My question I have is, I want to take the results and have them in a format where I can add everything up.
所以它会说出每种尺寸有多少,每种尺寸有多少以及价格。我假设我需要一个多维数组。
so it'll say how many sizes of each I have, how many of each size, and the price. I'm assuming i need a multidimentional array.
我目前有:
$('#order-submit').click(function(){
var size = [];
var price = [];
var quantity = [];
$.each($('.entry-dropdown'), function() {
size.push($(this).val());
});
console.log(size);
但这一切都给了我只是一个阵列:[XL,3,15,3XL,1,15](当我测试它时)
But all this give me was a single array: ["XL", "3", "15", "3XL", "1", "15"] (when I tested it)
以可用格式获取数据的最佳方法是什么?
What would be the best way to get that data in a useable format?
我希望这是有道理的!
谢谢
I hope that makes sense!Thanks
推荐答案
你必须遍历每个动态销售
元素。然后,循环遍历子节点,将值添加到子数组。
You have to loop through each dynamic-sale
element. Then, loop through the children, and add the values to a sub-array.
var result = []; // <-- Main array
$(".dynamic-sale").each(function(){
var individual_result = []; // <-- "sub"-array
$(this).find("select").each(function(){
individual_result.push($(this).val());
})
result.push(individual_result); // <-- Add "sub" array to results
});
此方法返回一个数组,其中包含一个大小,价格和数组的数组。数量
。
This method returns an array, consisting of an array with size, price and quantity
.
如果您想将这些值放在单独的数组中,请使用:
If you want to put these values in a separate array, use:
var size = [],
price = [],
quantity = [];
$(".dynamic-sale").each(function(){
var selects = $(this).find("select");
size.push(selects.eq(0).val());
price.push(selects.eq(1).val());
quantity.push(selects.eq(2).val());
});
这篇关于使用动态表单jquery创建多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!