2

jQueryセレクターをループして、フォーム要素の値をコンテナ変数に割り当てようとしています。コンテナ変数は、JSONとしてさらにアクションに渡すことができます。

group1、group2、および group3 という名前の 3 つのグループのチェックボックスがあり、それぞれが独自の div にあるとします。

これは疑似コードですが、次のようなものです。

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?
    });
}

したがって、基本的に、チェックされたアイテムの値をオブジェクトの適切なアイテムに追加したいと考えていますdatagroup1divをループしている場合、各グループのチェックボックスdata.group1data.group2などに追加されます。

data.indexとして評価するにはどうすればよいdata.group1ですか?

4

2 に答える 2

6

単純な:

data[index]

.......

于 2012-11-05T22:48:58.493 に答える
0

ES6 JavaScript 機能を使用できる場合は、Computed Property Namesを使用してこれを非常に簡単に処理できます。

var request = {id : 1236};
var responseMessage = 'The response message';

var resp = {responses: {[request.id]: responseMessage}};

[request.id] オブジェクト キーには動的な値があります

于 2018-01-16T11:00:35.073 に答える