如何检查jQuery中是否已选中复选框?

六月ゝ 毕业季﹏ 提交于 2020-10-03 01:28:52

问题:

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属性?


解决方案:

参考一: https://stackoom.com/question/3mZk/如何检查jQuery中是否已选中复选框
参考二: https://oldbug.net/q/3mZk/How-to-check-whether-a-checkbox-is-checked-in-jQuery
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!