2016-08-01 43 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()代替