I need to check the checked
property of a checkbox and perform an action based on the checked property using jQuery. (我需要检查复选框的checked
属性,并使用jQuery根据checked属性执行操作。)
For example, if the age checkbox is checked, then I need to show a textbox to enter age, else hide the textbox. (例如,如果选中了年龄复选框,那么我需要显示一个文本框来输入年龄,否则隐藏该文本框。)
But the following code returns false
by default: (但是以下代码默认情况下返回false
:)
if ($('#isAgeSelected').attr('checked')) { $("#txtAge").show(); } else { $("#txtAge").hide(); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" id="isAgeSelected"/> <div id="txtAge" style="display:none"> Age is selected </div>
How do I successfully query the checked
property? (如何成功查询checked
属性?)
ask by community wiki translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…