【问题标题】:Dropdown checkboxes and unchecked parents element?下拉复选框和未选中的父元素?
【发布时间】:2016-06-29 23:14:50
【问题描述】:

我对 jquery 很感兴趣,这就是我尝试在它上发展自己的原因。

我有一些问题要解决,但我的大脑已经停止了,因为我为我的实际工作做了很多工作。

demo

您看到的链接是我开发自己的项目。但我不能做的最后一件事是如何切换下拉我的复选框,它有子 ul 元素。我的意思是我想下拉我的嵌套复选框

如果我单击父元素,则必须检查并打开子元素(到目前为止还可以),但不必检查父元素。我的意思是我不想检查父元素是否有子元素。

我的代码

html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>No Title</title>
</head>
<body> 

    <div class="new-checkbox">
    <ul>
      <li>
        <input type="checkbox" id="input1">
        <label for="input1">kategori <strong>(1)</strong>
        </label>
        <ul>
          <li>
            <input type="checkbox" id="input11">
            <label for="input11">kategori<strong>(11)</strong>
            </label>
          </li>
          <li>
            <input type="checkbox" id="input12">
            <label for="input12">kategori <strong>(12)</strong>
            </label>
          </li>
          <li>
            <input type="checkbox" id="input13">
            <label for="input13">kategori <strong>(13)</strong>
            </label>
          </li>
        </ul>
      </li>
      <li>
        <input type="checkbox" id="input2">
        <label for="input2">kategori <strong>(2)</strong>
        </label>
      </li>
      <li>
        <input type="checkbox" id="input3">
        <label for="input3">kategori <strong>(3)</strong>
        </label>
        <ul>
          <li>
            <input type="checkbox" id="input31">
            <label for="input31">kategori <strong>(31)</strong>
            </label>
          </li>
          <li>
            <input type="checkbox" id="input32">
            <label for="input32">kategori <strong>(32)</strong>
            </label>
          </li>
          <li>
            <input type="checkbox" id="input33">
            <label for="input33">kategori <strong>(33)</strong>
            </label>
            <ul>
              <li>
                <input type="checkbox" id="input331">
                <label for="input331">kategori <strong>(331)</strong>
                </label>
              </li>
              <li>
                <input type="checkbox" id="input332">
                <label for="input332">kategori <strong>(332)</strong>
                </label>
              </li>
              <li>
                <input type="checkbox" id="input333">
                <label for="input333">kategori <strong>(333)</strong>
                </label>
              </li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
</div><!-- new checkbox-->

<script type="text/javascript" src="https://cdn.anitur.com.tr/js/jquery-1.8.2.min.js" ></script>

</body>
</html>

css

.new-checkbox ul {
  padding: 0;
  margin: 0;
  list-style: none;
  margin-left: 30px;
  font: normal 11px/16px"Segoe UI", Arial, Sans-serif;
}

.new-checkbox ul:first-child {
  margin-left: 0;
}

.new-checkbox ul li {
  margin: 3px 0;
}

.new-checkbox input[type="checkbox"] {
  display: none;
}

.new-checkbox label {
  cursor: pointer;
}

.new-checkbox input[type="checkbox"] + label:before {
  border: 1px solid #ffffff;
  content: "\00a0";
  display: inline-block;
  font: 16px/1em sans-serif;
  height: 13px;
  width: 13px;
  margin: 2px .25em 0 0;
  padding: 0;
  vertical-align: top;
  border: solid 1px #1375b3;
  color: #1375b3;
  opacity: .50;
}

.new-checkbox input[type="checkbox"]:checked + label:before {
  background: #fff;
  color: #1375b3;
  content: "\2714";
  text-align: center;
  box-shadow: 0 0 2px rgba(0, 0, 0, .25) inset;
  opacity: 1;
}

.new-checkbox input[type="checkbox"]:checked + label:after {
  font-weight: bold;
}

.new-checkbox ul li:before {
  content: "\25b6";
  display: inline-block;
  margin: 2px 0 0;
  width: 13px;
  height: 13px;
  vertical-align: top;
  text-align: center;
  color: #e74c3c;
  font-size: 8px;
  line-height: 13px;
  cursor: pointer;
}


.new-checkbox li {
  -webkit-user-select: none;
  -moz-user-select: none;
  user-select: none;
}

.new-checkbox input[type="checkbox"][id]:checked ~ li::before {
  content: "\25bc";
}

.new-checkbox li ul {
  display: none;
}

.new-checkbox li.has-checked > ul {
  display: block;
}
.addDownicon li::before {
    content: "\25bc" !important;
}

jquery

$(document).ready(function() {
  $('.new-checkbox input[type=checkbox]').on("change", function() {
    var checked = this.checked,
      $li = $(this).parent();
    $li.find('input[type=checkbox]').prop('checked', checked).parent().toggleClass('has-checked', checked);

    $li.parentsUntil('.new-checkbox', 'li').each(function() {
      var $checks = $(this).find('ul input[type=checkbox]');
      $(this).children('input[type=checkbox]').prop('checked', !$checks.filter(':not(:checked)').length);

      $(this).toggleClass('has-checked', $checks.is(':checked'));
    });

  });


});

【问题讨论】:

  • "但不必检查父元素。我的意思是如果有子元素,我不想检查父元素。"你能重写或解释一下吗?我不明白。
  • 你能改写你的问题吗?这样就真的不清楚你想要完成什么
  • 如果我点击父元素我的父元素和子元素被检查..但我希望我的父母元素不被检查是否有子元素(对不起我的英语)@JohannesJander
  • 所以如果我点击一个有子元素的父元素,它应该保持未选中状态,但是如果我点击一个没有子元素的父元素,它应该被选中?
  • @user3398922 我想为我的嵌套复选框制作下拉列表,如下所示jeasyui.com/tutorial/tree/tree4_demo.html

标签: javascript jquery html css checkbox


【解决方案1】:

我必须承认,我仍然不能 100% 确定我理解 - 可能是因为我不是英语母语人士,并且很难阅读复杂的问题,但我尝试了:

如果有孩子,您可以添加一个查找孩子并设置类has-children 的函数:

$('.new-checkbox ul')
   .find("input[type=checkbox]")
   .parent()
   .each(function(i, elem) {
     if ($(elem).find("ul").length > 0) {
       $(this).addClass('has-children');
     }
});

然后添加一个 CSS 规则,去除那些 &lt;input&gt; 标记上的人工复选框,其中父 &lt;li&gt; 具有类 has-children 设置:

.new-checkbox .has-children > input[type="checkbox"]:checked + label:before {
  content: " ";
}

我创建了an updated JSBin demo 来显示结果。

【讨论】:

  • 我很感谢你,是的,完全正确,但如果我点击标签,则无需检查,但如果我点击标签,则必须检查,如果我点击第二次复选框,复选框无法正常工作是不是切换(选中,未选中)
  • 对不起,我不明白:/
  • 您的演示链接..当我单击子复选框时,它们没有被取消选中或再次选中:/
  • 好的,我明白你的意思了,我更新了演示:jsbin.com/fedoxakoge/1/edit?css,js,output
猜你喜欢
  • 1970-01-01
  • 2010-12-20
  • 1970-01-01
  • 2016-07-06
  • 1970-01-01
  • 1970-01-01
  • 2012-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多