将结果存储为新数组
Store results as new array
我正在构建一个包含多个屏幕(仅被 <section>
拆分)的调查问卷,并且需要将每个屏幕复选框结果存储为一个数组以用于查询。
我认为使用 .each() 和动态分配的变量名可以完成这项工作,但我的代码没有按部分拆分数组。
知道如何做到这一点吗?
$('.product_selector .submit').click(function(){
i = 1;
$('.product_selector section').each(function(){
section[i] = $("ul li input:checkbox:checked").map(function () {
return $(this).attr('name');
}).get();
i++;
});
});
我认为您可能需要像这样编写代码。
$('.product_selector .submit').click(function () {
var result = []; // a variable to store the data.
$('.product_selector section').each(function(){
// a variable to have each attribute.
// the variable will have at least a blank array if none of the options are checked. In your previous code, if a section has no checkbox checked, then there wont be data for that section.
var res = [];
$(this).find("ul li input:checkbox:checked").each(function () {
res.push(this.getAttribute('name'));
});
result.push(res);
});
console.log(result);});
我认为评论可能是解释性的。
我正在构建一个包含多个屏幕(仅被 <section>
拆分)的调查问卷,并且需要将每个屏幕复选框结果存储为一个数组以用于查询。
我认为使用 .each() 和动态分配的变量名可以完成这项工作,但我的代码没有按部分拆分数组。
知道如何做到这一点吗?
$('.product_selector .submit').click(function(){
i = 1;
$('.product_selector section').each(function(){
section[i] = $("ul li input:checkbox:checked").map(function () {
return $(this).attr('name');
}).get();
i++;
});
});
我认为您可能需要像这样编写代码。
$('.product_selector .submit').click(function () {
var result = []; // a variable to store the data.
$('.product_selector section').each(function(){
// a variable to have each attribute.
// the variable will have at least a blank array if none of the options are checked. In your previous code, if a section has no checkbox checked, then there wont be data for that section.
var res = [];
$(this).find("ul li input:checkbox:checked").each(function () {
res.push(this.getAttribute('name'));
});
result.push(res);
});
console.log(result);});
我认为评论可能是解释性的。