【问题标题】:How to make only one checkbox checked in multiple checkboxes based on specific parent in jquery如何根据jquery中的特定父项在多个复选框中仅选中一个复选框
【发布时间】:2019-05-01 20:54:39
【问题描述】:

这里我有多个checkboxes,其结构类似于父子关系。这里我只能根据父项检查一个复选框,假设Parent1我检查Child11时再次检查Child12,@987654326 @ 应该取消选中,类似于单选按钮。

同样的事情应该发生在Parent2,但是如果我check/uncheckParent2 中的任何孩子,parent1 的孩子不应该被打扰。

谁能帮帮我,这是下面的代码

HTML

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<div class="details">
<ul>
<li>Parent1</li>
<li>
<ul>
<li><input type="checkbox"><span>Child11</span></li>
<li><input type="checkbox"><span>Child12</span></li>
</ul>
</li>
</ul>

<ul>
<li>Parent2</li>
<li>
<ul>
<li><input type="checkbox"><span>Child21</span></li>
<li><input type="checkbox"><span>Child22</span></li>
<li><input type="checkbox"><span>Child23</span></li>
</ul>
</li>
</ul>
</div>

CSS

ul li{
  list-style-type:none;
  }

【问题讨论】:

标签: java jquery html


【解决方案1】:

$(".p1chb").change(function()
                                  {
                                      $(".p1chb").prop('checked',false);
                                      $(this).prop('checked',true);
                                  });
                                  
                                  ///above for parent 1, below for parent 2.
 $(".p2chb").change(function()
                                  {
                                      $(".p2chb").prop('checked',false);
                                      $(this).prop('checked',true);
                                  });
                                  
ul li{
  list-style-type:none;
  }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<div class="details">
<ul>
<li>Parent1</li>
<li>
<ul>
<li><input type="checkbox" id="Child11" name="Parent1" value="Child11" class="p1chb"
         checked>
  <label for="Child11">Child11</li>
<li><input type="checkbox" id="Child12" name="Parent1" value="Child12" class="p1chb"
         unchecked>
  <label for="Child12">Child12</label></li>
</ul>
</li>
</ul>

<ul>
<li>Parent2</li>
<li>
<ul>
<li><input type="checkbox" id="Child21" name="Parent2" value="Child21" class="p2chb"
         checked>
  <label for="Child21">Child21</label></li>
<li><input type="checkbox" id="Child22" name="Parent2" value="Child22" class="p2chb"
         unchecked>
  <label for="Child21">Child22</label></li>
<li><input type="checkbox" id="Child23" name="Parent2" value="Child23" class="p2chb"
         unchecked>
  <label for="Child23">Child23</label></li>
</ul>
</li>
</ul>
</div>

<div>

【讨论】:

  • 感谢您的回答,我们可以用jquery为复选框做吗
  • 用 jquery 改成复选框
  • 感谢您的回答。p1chb 这个类我可能没有,因为所有
      都是在我的项目中动态生成的,所以可能有 n 个
        ,我的意思是父子,这里的例子是 2,但在我的项目中它可能是 3、4、5 等等,这取决于一些 API。
      • 主要是做例如,只要确保复选框的不同部分的类不一样,即父1和父2应该有不同的输入类名。
      • 我明白你的意思,但即使我也不知道会有多少父子,它们会根据某些条件生成。那么在这种情况下如何编写脚本
      猜你喜欢
      • 1970-01-01
      • 2019-01-03
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 2023-03-13
      • 1970-01-01
      相关资源
      最近更新 更多