如何在jquery中获取复选框选定的对象
How to get checkbox selected object in jquery
嗨,到目前为止,我的代码 https://jsfiddle.net/Harsh343/LcL38dos/5/
如何获取所选复选框的对象
例如,我希望在单击提交后,根据检查的支票簿进行以下输出。
"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"
}
您可以执行一个简单的基于迭代的解决方案,例如
var data = {
"getafixTest2": {
"testCategory": {
"tests": [
"stress",
"common",
"tests"
],
"api": [
"baremetal",
"orchestration"
],
"scenario": [
"something"
]
},
"testDescription": "this is a getafix cloud test",
"testName": "getafixTest2",
"cloudName": "getafix"
}
};
for (name in data) {
categoryObject = data[name].testCategory;
var testName = name;
var testDescription = data[name].testDescription;
var cloudName = data[name].cloudName;
}
dataHtml = 'Test Name: <input id="testNameId" type="text" value="' + testName + '"/><br><br>';
dataHtml += '<input id="cloudNameId" type="hidden" value="' + cloudName + '"/>';
dataHtml += '<input id="categoryObject" type="hidden" value="' + testName + '"/><br>';
dataHtml += 'Test Description: <input type="text" id="testDescriptionId" value="' + testDescription + '"/><br><br>';
// dataHtml += '<select id="users_list" name="users_list" multiple="multiple" size="15">';
dataHtml += '<ul>';
for (catName in categoryObject) {
categoryName = catName;
categoryType = categoryObject[catName];
$("#categoryName").text(categoryName);
dataHtml += '<li><input type="checkbox" name="parent_list[]" value="' + categoryName + '" />' + categoryName;
dataHtml += '<ul>';
for (type in categoryType) {
dataHtml += '<li><input type="checkbox" name="child_list[]" value="' + categoryType[type] + '" />' + categoryType[type] + '</li>';
}
dataHtml += '</ul></li>'
}
dataHtml += '<input type="button" id="submit_data" value="Submit data" />';
$('body').append(dataHtml)
$('input[type=checkbox]').click(function() {
$(this).parent().find('li input[type=checkbox]').prop('checked', $(this).is(':checked'));
var sibs = false;
$(this).closest('ul').children('li').each(function() {
if ($('input[type=checkbox]', this).is(':checked')) sibs = true;
})
$(this).parents('ul').prev().prop('checked', sibs);
});
$("#submit_data").click(function() {
var obj = {};
$('input[name="parent_list[]"]:checked').each(function() {
obj[this.nextSibling.nodeValue] = $(this).next().find('input:checked').map(function() {
return this.nextSibling.nodeValue;
}).get();
})
console.table(obj);
$("#demo").html(JSON.stringify(obj, null, 2));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="demo">
</pre>
您可以使用以下代码:
$('input[type=checkbox]:checked')
这将返回所有选定的复选框。
相关文章:
- angularjs 选中复选框时隐藏具有特定类的对象
- 选择一个从 mysql 返回选中/未选中复选框的对象
- Javascript 从嵌套的 UL LI 复选框生成递归 JSON 对象
- 根据复选框输入过滤深层对象
- 如何将所有复选框放入一个对象数组并标记选中的复选框
- 从复选框创建具有复杂变量的对象
- 选中加载时对象数组中的复选框
- 选中复选框(如果 json 对象中存在)
- 如何在jquery中获取复选框选定的对象
- 道场复选框对象和 ArcGIS JS API
- 将复选框绑定到 AngularJs 中的对象值
- 如何使用 Javascript 或 jQuery 将输入类型复选框存储到 json 对象中
- 语法错误,无法识别的表达式:[对象 HTMLDivElement]输入:复选框[名称=]:选中
- 从复选框循环循环遍历 json 对象
- 将复选框链接到 Javascript 中的对象
- Jquery-如何获取选中复选框的Jquery对象
- 使用AngularJS复选框过滤对象属性
- javascript使用复选框迭代对象数组
- 选中复选框时更改父对象的背景
- 使用复选框将JSON结果解析为对象