一组单选按钮包含一组复选框

时间:2013-12-19 18:14:10

标签: javascript jquery checkbox radio-button

我正在尝试制作一个包含一组复选框的单选按钮列表,这些复选框又包含另一个组复选框:

当我在一个单选按钮中使用时,我的代码工作正常。如果我选择另一个JS返回一些奇怪的东西,我不知道如何解决它。

这是我的代码:

HTML

  <ul>
    <li class="radio">
      <input type="radio" name="level-1">submenu1.1
      <ul>
        <li>
          <input type="checkbox" name="level-2">submenu1.1.1
          <ul>
            <li><input type="checkbox" name="level-3">submenu1.1.1.1</li>
            <li><input type="checkbox" name="level-3">submenu1.1.1.2</li>
            <li><input type="checkbox" name="level-3">submenu1.1.1.3</li>
          </ul>
        </li>
        <li>
          <input type="checkbox" name="level-2">submenu1.1.2
          <ul>
            <li><input type="checkbox" name="level-3">submenu1.1.2.1</li>
            <li><input type="checkbox" name="level-3">submenu1.1.2.2</li>
            <li><input type="checkbox" name="level-3">submenu1.1.2.3</li>
          </ul>
        </li>
        <li>
          <input type="checkbox" name="level-2">submenu1.1.3
          <ul>
            <li><input type="checkbox" name="level-3">submenu1.1.3.1</li>
            <li><input type="checkbox" name="level-3">submenu1.1.3.2</li>
            <li><input type="checkbox" name="level-3">submenu1.1.3.3</li>
          </ul>
        </li>
        <li>
          <input type="checkbox" name="level-2">submenu1.1.4
          <ul>
            <li><input type="checkbox" name="level-3">submenu1.1.4.1</li>
            <li><input type="checkbox" name="level-3">submenu1.1.4.2</li>
            <li><input type="checkbox" name="level-3">submenu1.1.4.3</li>
          </ul>
        </li>
      </ul>
    </li>

    <br>

    <li class="radio">
      <input type="radio" name="level-1" value="submenu">submenu1.2
      <ul>
        <li>
          <input type="checkbox" name="level-2">submenu1.2.1
          <ul>
            <li><input type="checkbox" name="level-3">submenu1.2.1.1</li>
            <li><input type="checkbox" name="level-3">submenu1.2.1.2</li>
            <li><input type="checkbox" name="level-3">submenu1.2.1.3</li>
          </ul>
        </li>
        <li>
          <input type="checkbox" name="level-2">submenu1.2.2
          <ul>
            <li><input type="checkbox" name="level-3">submenu1.2.2.1</li>
            <li><input type="checkbox" name="level-3">submenu1.2.2.2</li>
            <li><input type="checkbox" name="level-3">submenu1.2.2.3</li>
          </ul>
        </li>
        <li>
          <input type="checkbox" name="level-2">submenu1.2.3
          <ul>
            <li><input type="checkbox" name="level-3">submenu1.2.3.1</li>
            <li><input type="checkbox" name="level-3">submenu1.2.3.2</li>
            <li><input type="checkbox" name="level-3">submenu1.2.3.3</li>
          </ul>
        </li>
        <li>
          <input type="checkbox" name="level-2">submenu1.2.4
          <ul>
            <li><input type="checkbox" name="level-3">submenu1.2.4.1</li>
            <li><input type="checkbox" name="level-3">submenu1.2.4.2</li>
            <li><input type="checkbox" name="level-3">submenu1.2.4.3</li>
          </ul>
        </li>
      </ul>
    </li>

    <br>

    <li class="radio">
      <input type="radio" name="level-1">submenu1.3
      <ul>
        <li>
          <input type="checkbox" name="level-2">submenu1.3.1
          <ul>
            <li><input type="checkbox" name="level-3" value="submenu">submenu1.3.1.1</li>
            <li><input type="checkbox" name="level-3" value="submenu">submenu1.3.1.2</li>
            <li><input type="checkbox" name="level-3" value="submenu">submenu1.3.1.3</li>
          </ul>
        </li>
        <li>
          <input type="checkbox" name="level-2">submenu1.3.2
          <ul>
            <li><input type="checkbox" name="level-3" value="submenu">submenu1.3.2.1</li>
            <li><input type="checkbox" name="level-3" value="submenu">submenu1.3.2.2</li>
            <li><input type="checkbox" name="level-3" value="submenu">submenu1.3.2.3</li>
          </ul>
        </li>
        <li>
          <input type="checkbox" name="level-2">submenu1.3.3
          <ul>
            <li><input type="checkbox" name="level-3" value="submenu">submenu1.3.3.1</li>
            <li><input type="checkbox" name="level-3" value="submenu">submenu1.3.3.2</li>
            <li><input type="checkbox" name="level-3" value="submenu">submenu1.3.3.3</li>
          </ul>
        </li>
        <li>
          <input type="checkbox" name="level-2">submenu1.3.4
          <ul>
            <li><input type="checkbox" name="level-3" value="submenu">submenu1.3.4.1</li>
            <li><input type="checkbox" name="level-3" value="submenu">submenu1.3.4.2</li>
            <li><input type="checkbox" name="level-3" value="submenu">submenu1.3.4.3</li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>

JS

$(document).ready(function() {
  $("li:not(.radio)").hide();
  $("input[name='level-1']").change(function() {
    console.log(this);
    $("li:not(.radio)").hide();
    var $val = $(this).next("ul").children("li");
    $val.slideToggle("fast");
    $val.next("ul").children("li").hide();

    $("input[type=checkbox][name='level-2']").click(function (event) {
      $(this).next("ul").children("li").slideToggle("fast");
      var checked = $(this).is(':checked');
      if (!checked) {
        $(this).parent().find("input[type=checkbox][name='level-3']").prop("checked", false);
      }
    });
  });
});

http://jsfiddle.net/benatespina/Et95z/

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您在level-2更改事件中触发了level-1更改事件。

单独写下来。

写:

  $("li:not(.radio)").hide();
  $("input[name='level-1']").change(function () {
      console.log(this);
      $("li:not(.radio)").hide();
      var $val = $(this).next("ul").children("li");
      $val.slideToggle("fast");
      $val.next("ul").children("li").hide();
      $("input[type=checkbox][name='level-2']:checked").removeAttr("checked");
  });
  $("input[type=checkbox][name='level-2']").change(function (event) {
      $(this).next("ul").find("li").slideToggle("fast");
      var checked = $(this).is(':checked');
      if (!checked) {
          $(this).parent().find("input[type=checkbox][name='level-3']").prop("checked", false);
      }
  });

Updated fiddle here.