我需要检查复选框的checked
属性,并使用jQuery根据已检查的属性执行操作。
例如,如果选中了年龄复选框,那么我需要显示一个文本框以输入年龄,否则隐藏该文本框。
但是以下代码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>
如何成功查询checked
属性?
Though you have proposed a JavaScript solution for your problem (displaying a
textbox
when acheckbox
ischecked
), this problem could be solved just by css. With this approach, your form works for users who have disabled JavaScript.Assuming that you have the following HTML:
You can use the following CSS to achieve the desired functionality:
For other scenarios, you may think of appropriate CSS selectors.
Here is a Fiddle to demonstrate this approach.