首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在jquery中获取选中对象的复选框

如何在jquery中获取选中对象的复选框
EN

Stack Overflow用户
提问于 2016-03-10 12:54:17
回答 2查看 681关注 0票数 2

嗨,我的代码是目前为止的https://jsfiddle.net/Harsh343/LcL38dos/5/

如何获取所选复选框的对象

例如,在单击submit之后,我希望下面的输出基于支票簿检查。

代码语言:javascript
复制
"getafixTest2": {
  "testCategory": {
    "tests": [
      "stress",
      "common",
      "tests"
    ],
    "api": [
      "baremetal",
      "orchestration"
    ],
    "scenario": [
      "something"
    ]
  },
  "testDescription": "this is a getafix cloud test",
  "testName": "getafixTest2",
  "cloudName": "getafix"
}

当前,当我选择tests父复选框时,我将看到下面的内容

代码语言:javascript
复制
Object {tests: Array[3], tests,api: Array[5]}

但我想要这个

代码语言:javascript
复制
Object {tests: Array[3], api: Array[2]}

 "tests": [
    "stress",
    "common",
    "tests"
  ],
  "api": [
    "baremetal",
    "orchestration"
  ]

任何帮助都是非常感谢的。

怎样才能得到这样的输出

代码语言:javascript
复制
 "getafixTest2": {
      "testCategory": {
        "tests": [
          "stress",
          "common",
          "tests"
        ],
        "api": [
          "baremetal",
          "orchestration"
        ],
        "scenario": [
          "something"
        ]
      },
      "testDescription": "this is a getafix cloud test",
      "testName": "getafixTest2",
      "cloudName": "getafix"
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-10 13:10:40

您可以执行一个简单的基于迭代的解决方案,如

代码语言:javascript
复制
  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));

  });
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="demo">

</pre>

票数 0
EN

Stack Overflow用户

发布于 2016-03-10 13:10:31

您可以使用以下代码:

代码语言:javascript
复制
$('input[type=checkbox]:checked')

这将返回所有选定的复选框。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35917102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档