2016-08-01 48 views
0

后我想删除所有表行的用户点击了复选框后,除了与自身的复选框的表行,并在未选中该复选框再次显示表中的行后面。我的代码仅用于删除,但我需要在取消选中后再次显示表格。显示/隐藏表行点击复选框

function Showhidefunc(btndel) { 
    if (typeof(btndel) == "object") { 
    $('table tr td:first-child :checkbox:not(:checked)').closest('tr').remove(); 
    } else { 
     return false; 
    } 
} 

<thead> 
<tr> 
    <th>Select</th> 
    <th>Month</th> 
    <th>Username</th> 
    <th>Deduction</th> 
</tr> 
</thead> 
<tbody> 
<tr> 
    <td><input type="checkbox" onChange="Showhidefunc(this);"></td> 
    <td><?php echo $Name_row ['Month'];?></td> 
    <td><?php echo $Name_row ['Username'];?></td> 
    <td><?php echo $Name_row ['Total_Deduction'];?></td> 
</tr> 
</tbody> 

谢谢你们:)

+0

不要'一个.remove()'行 - 一旦你删除它们,他们走了。使用'.hide()''然后.show()' –

+1

你可以使用jQuery的[切换()](http://www.w3schools.com/jquery/eff_toggle.asp)。 –

+0

也帮助我,谢谢。 –

回答

2

不要在行上使用.remove(),因为这会删除它们;一旦你删除它们,它们就消失了,你无法将它们还原。

使用.hide()然后.show()

function Showhidefunc(chkbox) { 
    if ($(chkbox).is(":checked")) 
     $('table tr td:first-child :checkbox:not(:checked)').closest('tr').hide(); 
    } else { 
     $('table tr td:first-child :checkbox:not(:checked)').closest('tr').show(); 
    } 
    } 

更新:更新代码以匹配html和的if/else

版本与toggle更好:

function Showhidefunc(chkbox) { 
    $('table tr td:first-child :checkbox:not(:checked)').closest('tr').toggle(!$(chkbox).is(":checked")); 
    } 

顺便说一句,还有其他的方式来做到这一点,如使用.map()

+0

你工作得很好,非常感谢,:) –

0

function Showhidefunc(btndel) { if (typeof(btndel) == "object") { $('table tr td:first-child :checkbox:not(:checked)').closest('tr').remove(); } else { return false; } }

是卸下摆臂应在其他.hide()

你想要做相同的,但使用.show()代替