我想在选中复选框时隐藏表格行(内部有输入字段)。 我发现了一些有用的东西:
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 了解不多,但我确实需要这个
最佳答案
触发器 .change()附加事件后的事件:
$(function () {
$('#checkbox1, #checkbox2').change(function () {
var row = $(this).closest('tr').prev();
if (!this.checked)
row.fadeIn('slow');
else
row.fadeOut('slow');
}).change();
});
注意:我使代码更短。
关于javascript - 如果选中复选框,则显示或隐藏表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27621174/