在选中时显示和隐藏div,取消选中复选框

时间:2016-06-30 11:12:07

标签: javascript jquery html css

我想隐藏并在检查时显示div并取消选中复选框。我正在找父母并找到我要隐藏的div。但是代码没有运行

Jquery是:

$('.show-check').click(function() {
    if (this.checked) {    
        $(this).parent().find('.div-check').fadeIn('slow');
    } else
        $(this).parent().find('.div-check').fadeOut('slow');
});

HTML:

<div class="type-details">
  <span class="form-label">Logo:</span>
  <div class="switch">
    <label>
      <input type="checkbox" class="show-check" checked>
      <span class="lever"></span>
    </label>
  </div>
  <div class="landing-inputfile div-check">
    <div class="col-xs-12 no-padding">
      <div class="input-group">
        <input type="text" class="file-input" placeholder="No file" readonly>
        <label class="input-group-btn">
          <span class="btn btn-default btn-flat btn-basic2">
             UPLOAD <input type="file" style="display: none;">
          </span>
        </label>
      </div>
    </div>
  </div>
</div>

6 个答案:

答案 0 :(得分:5)

为什么您的代码不起作用

$(this).parent()会给你label - 而find('.div-check')不会返回任何内容。

使用 closest() - 获取具有所需子元素的公共父级,

$(this).closest('.type-details').find('.div-check').fadeIn('slow');

此外,我建议您使用change()事件代替click()复选框,如下所示:

$('.show-check').click(function() {

答案 1 :(得分:1)

使用此脚本

$('.show-check').click(function() {
  if ($(this).prop('checked')) {
    $(this).parents('.type-details').find('.div-check').fadeIn('slow');
  } else
    $(this).parents('.type-details').find('.div-check').fadeOut('slow');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div class="type-details">
  <span class="form-label">Logo:</span>
  <div class="switch">
    <label>
      <input type="checkbox" class="show-check" checked>
      <span class="lever"></span>
    </label>
  </div>
  <div class="landing-inputfile div-check">
    <div class="col-xs-12 no-padding">
      <div class="input-group">
        <input type="text" class="file-input" placeholder="No file" readonly>
        <label class="input-group-btn">
          <span class="btn btn-default btn-flat btn-basic2">
            UPLOAD <input type="file" style="display: none;">
          </span>
        </label>
      </div>
    </div>
  </div>
</div>

答案 2 :(得分:0)

使用$('input').is(':checked')

在您的示例中:$(this).is(':checked')

您必须使用parents()选择正确的包装器才能找到元素div-check

var $check = $(this).parents('.type-details').find('.div-check');
$('.show-check').click(function() {
   if ($(this).is(':checked')) {
       $check.fadeIn('slow');
   } else
       $check.fadeOut('slow');
});

答案 3 :(得分:0)

使用$(this).closest(".type-details").find('.div-check')获取div-check div并隐藏/显示它。

&#13;
&#13;
$('.show-check').click(function() {
    if (this.checked) {
        $(this).closest(".type-details").find('.div-check').fadeIn('slow');
    } else {
        $(this).closest(".type-details").find('.div-check').fadeOut('slow');
      }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div class="type-details">
  <span class="form-label">Logo:</span>
  <div class="switch">
    <label>
      <input type="checkbox" class="show-check" checked>
      <span class="lever"></span>
    </label>
  </div>
  <div class="landing-inputfile div-check">
    <div class="col-xs-12 no-padding">
      <div class="input-group">
        <input type="text" class="file-input" placeholder="No file" readonly>
        <label class="input-group-btn">
          <span class="btn btn-default btn-flat btn-basic2">
            UPLOAD <input type="file" style="display: none;">
          </span>
        </label>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

$('.show-check').click(function() {
    if ($(this).is(":checked")) {

        $(this).closest('.type-details').find('.div-check').fadeIn('slow');
    } else
        $(this).closest('.type-details').find('.div-check').fadeOut('slow');
});

答案 5 :(得分:0)

试试这段代码:

$('.show-check').click(function() {
    $(this).toggleClass('checked unchecked');
    if ($(this).hasClass('checked')) {
        $(this).parents('.type-details').find('.div-check').fadeIn('slow');
    } else {
        $(this).parents('.type-details').find('.div-check').fadeOut('slow');
    }
});

 <input type="checkbox" class="show-check checked" checked>