如果复选框被勾选,显示或隐藏表格行

4
当复选框被选中时,我希望隐藏一个包含输入字段的表格行。 我找到了一些可行的方法: HTML
<table>
    <tr id="row">
        <td><input type="text"></td>
        <td><input type="text"></td>
    </tr>
    <tr>
        <td><input type="checkbox" id="checkbox">Hide inputs</td>
    </tr>
</table>

脚本

$(document).ready(function () {
    $('#checkbox').change(function () {
        if (!this.checked) 
            $('#row').fadeIn('slow');
        else 
            $('#row').fadeOut('slow');
    });
});

示例

但是这只有在复选框还没有被选中的情况下才有效。所以,如果一开始复选框已经被选中,我想要隐藏表格行。我应该怎么做?

请注意,我对JavaScript不是很了解,但我真的需要这个。


这种类型的问题已经在这里被问了很多次了... - SomeShinyObject
@ChristopherW - 显然,谷歌已经成为一门失传的艺术。 - jalynn2
@jalynn2 看起来一般都有搜索功能,特别是在编程方面。 - SomeShinyObject
9个回答

5

在绑定事件之后触发.change()事件:

$(function () {
    $('#checkbox1, #checkbox2').change(function () {
        var row = $(this).closest('tr').prev();

        if (!this.checked)
            row.fadeIn('slow');
        else 
            row.fadeOut('slow');

    }).change();
});

注意:我会缩短代码。 jsfiddle

4

在最初注册事件后,只需调用更改事件:

$(document).ready(function () {
    $('#checkbox').change(function () {
        if (!this.checked) 
            $('#row').fadeIn('slow');
        else 
            $('#row').fadeOut('slow');
    });
    $('#checkbox').change();
});

2
Musefan的回答很棒,但下面还有另一种方法!

$(document).ready(function () {
($('#checkbox').prop('checked')==true) ? $('#row').fadeOut('slow'):$('#row').fadeIn('slow');
    $('#checkbox').change(function () {
        if (!this.checked) 
            $('#row').fadeIn('slow');
        else 
            $('#row').fadeOut('slow');
    });
});


2
我相信这就是您寻找的内容:

我相信这就是您在寻找的内容:

$(function() {
  var init = true;
  $('input[type="checkbox"]').change(function() {
    if (this.checked) {
      if (init) {
        $(this).prev().hide();
        init = false;
      } else $(this).prev().slideUp();
    } else $(this).prev().slideDown();
  }).change();
});
input[type='text'] {
  display: block;
  padding: 3px 5px;
  margin: 5px 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
  <input type="text" placeholder="Shown" />
  <input type="checkbox" />Hide input
</div>

<div>
  <input type="text" placeholder="Hidden" />
  <input type="checkbox" checked/>Hide input
</div>


1
在文档准备好后,只需调用更改函数即可。
 $('#checkbox').change();

像这样
$(document).ready(function () {

    $('#checkbox').change(function () {
        if (!this.checked) $('#row').fadeIn('slow');
        else $('#row').fadeOut('slow');
    });
    $('#checkbox').change();
});

这里是DEMO FIDDLE

1
通用解决方案,没有硬编码的id:

$('table :checkbox').change(function(e, speed) {
    speed = typeof speed == 'undefined' ? 'slow' : 0;
    $(this).closest('tr').prev()[this.checked ? 'fadeOut' : 'fadeIn'](speed);
}).trigger('change', [0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr id="row1">
        <td><input type="text"></td>
        <td><input type="text"></td>
    </tr>
    <tr>
        <td><input type="checkbox" id="checkbox1">Hide inputs</td>
    </tr>
        
    <tr id="row2">
        <td><input type="text"></td>
        <td><input type="text"></td>
    </tr>
    <tr>
        <td><input type="checkbox" id="checkbox2" checked>Hide inputs</td>
    </tr>
</table>


0

如果你真的想让复选框最初被选中,你可以最初将它们隐藏起来。

<table>
<tr id="row" style="display: none;">
    <td><input type="text"></td>
    <td><input type="text"></td>
</tr>
<tr>
    <td><input type="checkbox" id="checkbox" checked>Hide inputs</td>
</tr>
</table>
<script>
$(document).ready(function () {
$('#checkbox').change(function () {
    if (!this.checked) 
        $('#row').fadeIn('slow');
    else 
        $('#row').fadeOut('slow');
});
});
</script>

0
var showOrHideRow=fucntion(isChecked){
   if (isChecked) 
            $('#row').fadeOut('slow');
        else 
            $('#row').fadeIn('slow');
};

$(document).ready(function () {
showOrHideRow($('#checkbox').is(":checked"));

    $('#checkbox').change(function () {
        showOrHideRow(this.checked);
    });

});

0

$('#tbl_name tr').find('input:checkbox:checked').closest('tr').show(); $('#tbl_name tr').find('input:checkbox:unchecked').closest('tr').hide();


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接