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?
});
}
したがって、基本的に、チェックされたアイテムの値をオブジェクトの適切なアイテムに追加したいと考えていますdata
。group1
divをループしている場合、各グループのチェックボックスdata.group1
がdata.group2
などに追加されます。
data.index
として評価するにはどうすればよいdata.group1
ですか?