【问题标题】:how to hide textareas if checkboxes not checked - JQuery如果未选中复选框,如何隐藏文本区域 - JQuery
【发布时间】:2010-06-21 16:49:42
【问题描述】:

我正在尝试使用 jQuery 查看表单,并为每个复选框“如果选中”然后显示一个包含文本区域的 div。

复选框 id 'checkbiox_foo' 的每个 div 都是 id 'checkbox_foo_reasons'

我是 jquery 的菜鸟,所以我已经做到了这一点,但我无法选择 div 来隐藏或显示它。

$(document).ready(function() {
 $('#storySelection input').each(function(){
  if($(this).is(':checked') ){
   alert($('#'+this.id+'_reasons'));

  }
 });
});

感谢您的帮助。

干杯,

保罗

【问题讨论】:

  • 请包含您的 HTML 结构。它将帮助生成示例 jQuery 代码。

标签: jquery select html checkbox show


【解决方案1】:

没有看到你的 HTML,我不能确定,但​​你可能想要这样的东西:

$(function () {
  $('#storySelection input').click(function () {
    var $this = $(this);
    if ($this.is(':checked')) {
      $this.next('div').show();
    } else {
      $this.next('div').hide();
    }
  })
});

【讨论】:

    【解决方案2】:

    您可以使用.change().toggle() 来实现,如下所示:

    $(function() {
     $('#storySelection input').change(function() {
       $('#'+this.id+'_reasons').toggle(this.checked);
     });
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 2016-03-03
      • 2012-03-20
      • 1970-01-01
      相关资源
      最近更新 更多