gpt4 book ai didi

javascript - 在 JavaScript 中将对象键设置为动态值

转载 作者:行者123 更新时间:2023-11-29 16:18:16 24 4
gpt4 key购买 nike

我正在遍历 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.group1data.group2 等。

如何使 data.index 评估为 data.group1

最佳答案

简单如:

data[index]

.......

关于javascript - 在 JavaScript 中将对象键设置为动态值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13241642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com