【问题标题】:Jquery hiding/unhiding on check box change not behaving as expectedJquery隐藏/取消隐藏复选框更改未按预期运行
【发布时间】:2019-02-14 14:32:48
【问题描述】:

请原谅我的任何坏习惯,我已经玩了 48 小时的 html 和 jquery。

我正在尝试根据选中的复选框隐藏/取消隐藏输入字段。如果选中“无”框,则隐藏所有字段。

我在以下代码显示输入字段时遇到问题,直到复选框被选中/取消选中一次。然后他们开始表现得像他们应该的那样。我也可以通过取消选中其他框来使“无”框工作,但这不会像我希望的那样触发字段隐藏。

有什么建议吗?

TLDR:在选中相应的框之前,不应看到 DIV 字段。如果选中“无”框,则所有 DIV 字段都应消失。

$('#checkboxes-0').change(function() {
  if (this.checked)
    $('#blank_co').fadeIn('slow');
  else
    $('#blank_co').fadeOut('slow');

});

$('#checkboxes-1').change(function() {
  if (this.checked)
    $('#fresh_co').fadeIn('slow');
  else
    $('#fresh_co').fadeOut('slow');

});

$('#checkboxes-2').change(function() {
  if (this.checked)
    $('#marine_co').fadeIn('slow');
  else
    $('#marine_co').fadeOut('slow');

});

$('#checkboxes-3').change(function() {
  if (this.checked)
    $('#rain_co').fadeIn('slow');
  else
    $('#rain_co').fadeOut('slow');
});

var $others = $('input[name="checkboxes"]').not('#checkboxes-4')
$('#checkboxes-4').change(function() {
  if (this.checked) {
    $others.prop('checked', false)
  }
});
$others.change(function() {
  if (this.checked) {
    $('#checkboxes-4').prop('checked', false)
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="form-group">

  <label class="col-md-4 control-label" for="checkboxes">
    Select any desired backgrounds:</label>
  <div class="col-md-4">
    <label class="checkbox-inline" for="checkboxes-0">
          <input name="checkboxes" id="checkboxes-0" type="checkbox" value="blanks">
          Blanks
        </label>
    <label class="checkbox-inline" for="checkboxes-1">
          <input name="checkboxes" id="checkboxes-1" type="checkbox" value="fresh">
          Fresh Water
        </label>
    <label class="checkbox-inline" for="checkboxes-2">
          <input name="checkboxes" id="checkboxes-2" type="checkbox" value="marine">
          Marine Water
        </label>
    <label class="checkbox-inline" for="checkboxes-3">
          <input name="checkboxes" id="checkboxes-3" type="checkbox" value="rain">
          Rain
        </label>
    <label class="checkbox-inline" for="checkboxes-4">
          <input name="checkboxes" id="checkboxes-4" type="checkbox" value="none">
          None
        </label>
  </div>
</div>

<!-- Text input-->
<div id="blank_co" div class="form-group">
  <label class="col-md-4 control-label" for="blankbg">Blanks cutoff: (1-99)</label>
  <div class="col-md-4">
    <input name="blankbg" class="form-control input-md" id="blankbg" required="" type="text" placeholder="" value="10">

  </div>
</div>

<!-- Text input-->
<div id="fresh_co" div class="form-group">
  <label class="col-md-4 control-label" for="freshbg">Fresh water cutoff: (1-99)</label>
  <div class="col-md-4">
    <input name="freshbg" class="form-control input-md" id="freshbg" required="" type="text" placeholder="" value="10">

  </div>
</div>

<!-- Text input-->
<div id="marine_co" div class="form-group">
  <label class="col-md-4 control-label" for="marinebg">Marine water cutoff: (1-99)</label>
  <div class="col-md-4">
    <input name="marinebg" class="form-control input-md" id="marinebg" required="" type="text" placeholder="" value="10">

  </div>
</div>

<!-- Text input-->
<div id="rain_co" div class="form-group">
  <label class="col-md-4 control-label" for="rainbg">Rain water cutoff: (1-99)</label>
  <div class="col-md-4">
    <input name="rainbg" class="form-control input-md" id="rainbg" required="" type="text" placeholder="" value="10">

  </div>
</div>

【问题讨论】:

  • 您使用复选框的.change() 方法,该方法在其值更改后触发给定函数。因此,您必须首先单击它们。
  • 我会尽快更新我的问题。我将 style="display:none" 添加到 DIV 块中,除非选中复选框,否则它们默认不显示。但这并不能解决“无”框行为。
  • 页面加载、显示或隐藏时输入应该如何?
  • 当“无”未选中时会发生什么?应该所有显示表单组还是只显示那些在选中时消失的表单组?

标签: javascript jquery html checkbox onchange


【解决方案1】:

您需要首先获取包含输入的所有&lt;div&gt; 元素,然后在选中None 复选框时,调用每个&lt;div&gt; 以根据需要淡出。

<html>
    <title>
    </title>
    <head>
    </head>
    <body>
        <!-- Multiple Checkboxes (inline) -->
        <div class="form-group">
    
    
    <label class="col-md-4 control-label" for="checkboxes">
    Select any desired backgrounds:</label>
      <div class="col-md-4">
        <label class="checkbox-inline" for="checkboxes-0">
          <input name="checkboxes" id="checkboxes-0" type="checkbox" value="blanks">
          Blanks
        </label>
        <label class="checkbox-inline" for="checkboxes-1">
          <input name="checkboxes" id="checkboxes-1" type="checkbox" value="fresh">
          Fresh Water
        </label>
        <label class="checkbox-inline" for="checkboxes-2">
          <input name="checkboxes" id="checkboxes-2" type="checkbox" value="marine">
          Marine Water
        </label>
        <label class="checkbox-inline" for="checkboxes-3">
          <input name="checkboxes" id="checkboxes-3" type="checkbox" value="rain">
          Rain
        </label>
        <label class="checkbox-inline" for="checkboxes-4">
          <input name="checkboxes" id="checkboxes-4" type="checkbox" value="none">
          None
        </label>
      </div>
    </div>
    
    <!-- Text input-->
    <div id="blank_co" div class="form-group">
      <label class="col-md-4 control-label" for="blankbg">Blanks cutoff: (1-99)</label>  
      <div class="col-md-4">
      <input name="blankbg" class="form-control input-md" id="blankbg" required="" type="text" placeholder="" value="10">
    
      </div>
    </div>
    
    <!-- Text input-->
    <div id="fresh_co" div class="form-group">
      <label class="col-md-4 control-label" for="freshbg">Fresh water cutoff: (1-99)</label>  
      <div class="col-md-4">
      <input name="freshbg" class="form-control input-md" id="freshbg" required="" type="text" placeholder="" value="10">
    
      </div>
    </div>
    
    <!-- Text input-->
    <div id="marine_co" div class="form-group">
      <label class="col-md-4 control-label" for="marinebg">Marine water cutoff: (1-99)</label>  
      <div class="col-md-4">
      <input name="marinebg" class="form-control input-md" id="marinebg" required="" type="text" placeholder="" value="10">
    
      </div>
    </div>
    
    <!-- Text input-->
    <div id="rain_co" div class="form-group">
      <label class="col-md-4 control-label" for="rainbg">Rain water cutoff: (1-99)</label>  
      <div class="col-md-4">
      <input name="rainbg" class="form-control input-md" id="rainbg" required="" type="text" placeholder="" value="10">
    
      </div>
    </div>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $('#checkboxes-0').change(function(){
            if(this.checked)
                $('#blank_co').fadeIn('slow');
            else
                $('#blank_co').fadeOut('slow');
    
        });
    
          $('#checkboxes-1').change(function(){
            if(this.checked)
                $('#fresh_co').fadeIn('slow');
            else
                $('#fresh_co').fadeOut('slow');
    
        });
    
          $('#checkboxes-2').change(function(){
            if(this.checked)
                $('#marine_co').fadeIn('slow');
            else
                $('#marine_co').fadeOut('slow');
    
        });
    
          $('#checkboxes-3').change(function(){
            if(this.checked)
                $('#rain_co').fadeIn('slow');
            else
                $('#rain_co').fadeOut('slow');
      });
    
        var $others = $('input[name="checkboxes"]').not('#checkboxes-4')
        var $othersWrappers = $('div[id$="_co"]')
    $('#checkboxes-4').change(function () {
        if (this.checked) {
            $others.prop('checked', false)
            $othersWrappers.each(function( index ) {
              $(this).fadeOut('slow');
    });
        }
    });
    $others.change(function () {
        if (this.checked) {
            $('#checkboxes-4').prop('checked', false)
        }
    })
    
    
    });
    </script>
    </body>
    
    </html>

【讨论】:

  • 这成功了。我在想通过改变检查状态,它会触发听众,但我想事实并非如此。 @Laurent.S 您是在说复选框更改功能吗?
  • @Steve 我明白你在想什么。这不起作用的原因是 onchange 事件仅在用户手动更改所选值时触发,而不是在以编程方式更改值时触发。
【解决方案2】:

这可能是你需要的。

<body>
    <!-- Multiple Checkboxes (inline) -->
    <div class="form-group">


<label class="col-md-4 control-label" for="checkboxes">
Select any desired backgrounds:</label>
  <div class="col-md-4">
    <label class="checkbox-inline" for="checkboxes-0">
      <input name="checkboxes" id="checkboxes-0" type="checkbox" value="blanks">
      Blanks
    </label>
    <label class="checkbox-inline" for="checkboxes-1">
      <input name="checkboxes" id="checkboxes-1" type="checkbox" value="fresh">
      Fresh Water
    </label>
    <label class="checkbox-inline" for="checkboxes-2">
      <input name="checkboxes" id="checkboxes-2" type="checkbox" value="marine">
      Marine Water
    </label>
    <label class="checkbox-inline" for="checkboxes-3">
      <input name="checkboxes" id="checkboxes-3" type="checkbox" value="rain">
      Rain
    </label>
    <label class="checkbox-inline" for="checkboxes-4">
      <input name="checkboxes" id="checkboxes-4" type="checkbox" value="none">
      None
    </label>
  </div>
</div>

<!-- Text input-->
<div id="blank_co" div class="form-group hide">
  <label class="col-md-4 control-label" for="blankbg">Blanks cutoff: (1-99)</label>  
  <div class="col-md-4">
  <input name="blankbg" class="form-control input-md" id="blankbg" required="" type="text" placeholder="" value="10">

  </div>
</div>

<!-- Text input-->
<div id="fresh_co" div class="form-group hide">
  <label class="col-md-4 control-label" for="freshbg">Fresh water cutoff: (1-99)</label>  
  <div class="col-md-4">
  <input name="freshbg" class="form-control input-md" id="freshbg" required="" type="text" placeholder="" value="10">

  </div>
</div>

<!-- Text input-->
<div id="marine_co" div class="form-group hide">
  <label class="col-md-4 control-label" for="marinebg">Marine water cutoff: (1-99)</label>  
  <div class="col-md-4">
  <input name="marinebg" class="form-control input-md" id="marinebg" required="" type="text" placeholder="" value="10">

  </div>
</div>

<!-- Text input-->
<div id="rain_co" div class="form-group hide">
  <label class="col-md-4 control-label" for="rainbg">Rain water cutoff: (1-99)</label>  
  <div class="col-md-4">
  <input name="rainbg" class="form-control input-md" id="rainbg" required="" type="text" placeholder="" value="10">

  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $('#checkboxes-0').change(function(){
        if(this.checked)
            $('#blank_co').fadeIn('slow');
        else
            $('#blank_co').fadeOut('slow');

    });

      $('#checkboxes-1').change(function(){
        if(this.checked)
            $('#fresh_co').fadeIn('slow');
        else
            $('#fresh_co').fadeOut('slow');

    });

      $('#checkboxes-2').change(function(){
        if(this.checked)
            $('#marine_co').fadeIn('slow');
        else
            $('#marine_co').fadeOut('slow');

    });

      $('#checkboxes-3').change(function(){
        if(this.checked)
            $('#rain_co').fadeIn('slow');
        else
            $('#rain_co').fadeOut('slow');
  });

    var $others = $('input[name="checkboxes"]').not('#checkboxes-4')
$('#checkboxes-4').change(function () {
    if (this.checked) {
        $others.prop('checked', false)
    }
});
$others.change(function () {
    if (this.checked) {
        $('#checkboxes-4').prop('checked', false)
    }
})


});
</script>
	
	<script>

    //hide "div.hide" if "none" checked
		
		$(document).ready(function(){
    $('#checkboxes-4').change(function(){
        if(this.checked)
            $('div.hide').fadeOut('slow');
        else
            $('div.hide').fadeIn('slow');

    });
});
</script>
</body>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    相关资源
    最近更新 更多