Javascript 如何在jquery中获取选中的复选框对象
嗨,我的代码到目前为止 如何获取选中复选框的对象 例如,我希望在单击“提交”后,根据选中的支票簿输出以下内容Javascript 如何在jquery中获取选中的复选框对象,javascript,jquery,javascript-objects,Javascript,Jquery,Javascript Objects,嗨,我的代码到目前为止 如何获取选中复选框的对象 例如,我希望在单击“提交”后,根据选中的支票簿输出以下内容 "getafixTest2": { "testCategory": { "tests": [ "stress", "common", "tests" ], "api": [ "baremetal", "orchestration" ], "scenario": [ "som
"getafixTest2": {
"testCategory": {
"tests": [
"stress",
"common",
"tests"
],
"api": [
"baremetal",
"orchestration"
],
"scenario": [
"something"
]
},
"testDescription": "this is a getafix cloud test",
"testName": "getafixTest2",
"cloudName": "getafix"
}
当前,当我选中“测试和api父项”复选框时,我会看到下面的内容
Object {tests: Array[3], tests,api: Array[5]}
但是我想要这个
Object {tests: Array[3], api: Array[2]}
"tests": [
"stress",
"common",
"tests"
],
"api": [
"baremetal",
"orchestration"
]
非常感谢您的帮助
如何获得这样的输出
"getafixTest2": {
"testCategory": {
"tests": [
"stress",
"common",
"tests"
],
"api": [
"baremetal",
"orchestration"
],
"scenario": [
"something"
]
},
"testDescription": "this is a getafix cloud test",
"testName": "getafixTest2",
"cloudName": "getafix"
}
您可以使用以下代码:
$('input[type=checkbox]:checked')
您可以执行一个简单的基于迭代的解决方案,如
var数据={
“GetAfix测试2”:{
“测试类别”:{
“测试”:[
“压力”,
“普通”,
“测试”
],
“api”:[
“纯金属”,
“编排”
],
“情景”:[
“什么”
]
},
“testDescription”:“这是一个getafix云测试”,
“testName”:“getafixTest2”,
“cloudName”:“getafix”
}
};
for(数据中的名称){
categoryObject=data[name].testCategory;
var testName=name;
var testDescription=data[name].testDescription;
var cloudName=data[name].cloudName;
}
dataHtml='测试名称:
';
dataHtml+='';
dataHtml+='
';
dataHtml+=“测试描述:
”;
//dataHtml+='';
dataHtml+='';
for(categoryObject中的catName){
categoryName=catName;
categoryType=categoryObject[catName];
$(“#categoryName”).text(categoryName);
dataHtml+='- '+类别名称;
dataHtml+='
';
对于(键入类别类型){
dataHtml+='- '+categoryType[type]+'
';
}
dataHtml+='
'
}
dataHtml+='';
$('body').append(dataHtml)
$('input[type=checkbox]')。单击(函数(){
$(this.parent().find('li input[type=checkbox]).prop('checked',$(this.is(':checked'));
var-sibs=false;
$(this).closest('ul')。children('li')。each(function(){
如果($('input[type=checkbox]',this).is(':checked'))sibs=true;
})
$(this.parents('ul').prev().prop('checked',sibs);
});
$(“#提交#U数据”)。单击(函数(){
var obj={};
$('input[name=“parent_list[]”:选中')。每个(函数(){
obj[this.nextSibling.nodeValue]=$(this.next().find('input:checked').map(function()){
返回this.nextSibling.nodeValue;
}).get();
})
控制台.工作台(obj);
$(“#demo”).html(JSON.stringify(obj,null,2));
});代码>
我希望输出类似于我的数据对象的内容