2015-06-12 42 views
3
<td> 
<input id="check" type="checkbox" name="del_attachment_id[]" value="<?php echo $attachment['link'];?>"> 
</td> 

<td id="delete" hidden="true"> 
the file will be deleted from the newsletter 
</td> 

我想知道我怎麼能屬性「隱藏」更改爲false jQuery中,當複選框被選中或者未選中更改屬性爲「隱藏」。如何在jQuery的

+0

非常感謝你:) – user3314813

+0

可能的重複[如何啓用按鈕,當複選框點擊jQuery中?](http://stackoverflow.com/questions/3565632/how-to-enable-button-when-checkbox-點擊jquery) –

+0

@ user3314813,如果沒有答案的工作,或者你仍然面臨麻煩,讓我知道,所以我可以幫助 – AmmarCSE

回答

4

您可以使用jQuery ATTR方法

$("#delete").attr("hidden","true"); 
+0

把變更處理程序在你的答案,所以這將是一個完整的答案。然後,我將刪除我的 – AmmarCSE

9
$(':checkbox').change(function(){ 
    $('#delete').removeAttr('hidden'); 
}); 

注意,由於通過A.Wolff給小費,你應該使用removeAttr,而不是設置爲false。設置爲false時,該元素仍將隱藏。因此,刪除更有效。

+2

隱藏是一個布爾屬性,您需要將其刪除,而不是將其設置爲「false」。或者使用'.prop('hidden',false);' –

+0

@ A.Wolff,你的意思是attr不會工作還是別的什麼? – AmmarCSE

+0

@ A.Wolff,根據http://www.sitepoint.com/web-foundations/hidden-html-attribute/值爲'true'或'false' – AmmarCSE

1

使用prop()更新的隱藏屬性,change()辦理變更事件。

$('#check').change(function() { 
 
    $("#delete").prop("hidden", !this.checked); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
    <td> 
 
     <input id="check" type="checkbox" name="del_attachment_id[]" value="<?php echo $attachment['link'];?>"> 
 
    </td> 
 

 
    <td id="delete" hidden="true"> 
 
     the file will be deleted from the newsletter 
 
    </td> 
 
    </tr> 
 
</table>

6

A.沃爾夫帶領你在正確的方向。有幾個屬性不應該設置字符串值。您必須使用布爾值truefalse來切換它。

.attr("hidden", false)將刪除與使用.removeAttr("hidden")相同的屬性。

.attr("hidden", "false")不正確,標籤仍然隱藏。

您不應將hidden,checked,selected或其他幾個字符串設置爲任何字符串值來切換它。