我正在遍历一个jQuery选择器,并试图将表单元素的值赋给一个容器变量,该变量可以作为JSON传递给进一步的操作。
假设有三组复选框,每组复选框都在自己的div中,分别命名为group1、group2和group3。
这是伪代码,但类似于以下内容:
var data = {};
var groups = {
'group1': obj with a bunch of key val pairs for checkboxes,
'group2': another obj,
'group3': and another obj
}; // these define divs / groups of checkboxes
// create all the checkboxes and divs from the groups object somewhere in here
//now build a function to loop over the groups and get any checked boxes
function getInputs(){
$.each(groups, function(index, el) {
// this is where I am stuck
data.index = $('#' + index + ' input:checked'); // <-- how to do this?
});
}
因此,我希望将所选项目的值添加到data
对象中的正确项目中--例如。如果我们正在循环处理group1
div,则每个组的复选框将添加到data.group1
,data.group2
等。
如何使data.index
评估为data.group1
?