多个复选框使用jquery切换

时间:2015-02-10 19:39:01

标签: javascript jquery html

我有一个复选框列表,我希望在选中时切换内容。我用jQuery写了它,但我想以某种方式优化我的代码,我不必为每个复选框编写一个函数。



$(document).ready(function() {
  $("#purpose-1").click(function() {
    $("#purpose-detail-1").toggle(this.checked);
  }).triggerHandler('click');
  $("#purpose-2").click(function() {
    $("#purpose-detail-2").toggle(this.checked);
  }).triggerHandler('click');
  $("#purpose-3").click(function() {
    $("#purpose-detail-3").toggle(this.checked);
  }).triggerHandler('click');
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <input type="checkbox" id="purpose-1">Exhibition
    <ul id="purpose-detail-1">
      <li>
        <div>Title:</div>
        <div>
          <input type="text">
        </div>
      </li>
      <li>
        <div>Opening Date:</div>
        <div>
          <input type="text">
        </div>
      </li>
    </ul>
  </li>
  <li>
    <input type="checkbox" id="purpose-2">Publication
    <ul id="purpose-detail-2">
      <li>
        <div>Title:</div>
        <div>
          <input type="text">
        </div>
      </li>
    </ul>
  </li>
  <li>
    <input type="checkbox" id="purpose-3">Event
    <ul id="purpose-detail-3">
      <li>
        <div>Title:</div>
        <div>
          <input type="text">
        </div>
      </li>
      <li>
        <div>Event Date:</div>
        <div>
          <input type="text">
        </div>
      </li>
      <li>
        <div>Event Time:</div>
        <div>
          <input type="text">
        </div>
      </li>
    </ul>
  </li>
</ul>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

我会使用课程并使用next()

访问ul

&#13;
&#13;
$(document).ready(function() {
  $(".my-ul").hide();
  $(".my-checkbox").click(function() {
    // in case the 'ul' is next to the checkbox:
    // $(this).next('ul').slideToggle(this.checked);
    // in case the 'ul' is in the same element but not next to the checkox:
    $(this).parents('li').find('ul').slideToggle(this.checked);
    // or maybe even better:
    // $(this).closest('li').find('ul').slideToggle(this.checked);
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <p> <input type="checkbox" class="my-checkbox"><strong>Exhibition</strong> </p> <ul class="my-ul">
      <li>
        <div>Title:</div>
        <div>
          <input type="text">
        </div>
      </li>
      <li>
        <div>Opening Date:</div>
        <div>
          <input type="text">
        </div>
      </li>
    </ul>
  </li>
  <li>
    <input class="my-checkbox" type="checkbox" id="purpose-2">Publication
    <ul class="my-ul" id="purpose-detail-2">
      <li>
        <div>Title:</div>
        <div>
          <input type="text">
        </div>
      </li>
    </ul>
  </li>
  <li>
    <input class="my-checkbox" class="my-checkbox" type="checkbox" id="purpose-3">Event
    <ul class="my-ul" id="purpose-detail-3">
      <li>
        <div>Title:</div>
        <div>
          <input type="text">
        </div>
      </li>
      <li>
        <div>Event Date:</div>
        <div>
          <input type="text">
        </div>
      </li>
      <li>
        <div>Event Time:</div>
        <div>
          <input type="text">
        </div>
      </li>
    </ul>
  </li>
</ul>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用课程:

<input type="checkbox" class="purpose-checkbox" id="purpose-1">Exhibition
<ul class="purpose-detail" id="purpose-detail-1">
    ...
</ul>

然后在jQuery中:

$(".purpose").click(function() { // We choose elements with purpose class
    var $this = $(this); // The purpose checkbox clicked
    var purpose_detail = $(".purpose-detail", $this ); // Element with purpose-detail class inside the clicked checkbox
    purpose_detail.toggle( $this.prop("checked") );
});