我想要使用jquery在選中時始終隱藏元素,並在未選中時顯示該元素。之後做一些研究,我發現了「是」的屬性,所以我創建了一個簡單的HTML文件:Jquery複選框隱藏或顯示元素
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
if($("#s").is(':checked'))
$("#test").hide(); // checked
else
$("#test").show();
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p id="test">This is a paragraph.</p>
<p id="test">This is another paragraph.</p>
<input type="checkbox" id="s">Click me</input>
</body>
</html>
現在,出於某種原因,該jQuery是不起作用。任何幫助,將不勝感激請。我也試過:
if(document.getElementById('isAgeSelected').checked) {
$("#txtAge").show();
} else {
$("#txtAge").hide();
}
而且這也行不通。
請注意,您要關閉,你從來沒有開了''
可能重複[設置「選中」的複選框與jQuery?](http://stackoverflow.com/questions/426258/setting-checked-for-a-checkbox-with-jquery) –