如何检查一个复选框是否选中与JQuery? [英] How do I check if a checkbox is checked with JQuery?
本文介绍了如何检查一个复选框是否选中与JQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果用户选中了网页上的复选框,我将尝试允许点击功能。否则,我想向#additional_foreign按钮添加一个类。
I am trying to allow a click function if the user has checked a checkbox on the page. Otherwise, I want to add a class to the #additional_foreign button.
我想我很近,但似乎没有工作。
这是我的代码:
I think I am close, but it doesn't seem to be working. Here is my code:
if($('#foreign_checkbox').attr('checked')) {
$('#additional_foreign').click(function() {
alert('This click function works');
});
} else {
$('#additional_foreign').addClass('btn_disable');
}
当我选中复选框时,它不允许点击功能,我取消选中该复选框,它也不会添加类应该的。
When I check the checkbox, it doesn't allow the click function and when I uncheck the checkbox, it also doesn't add the class as it should.
我做错了什么?
EDIT:这是我的HTML以供澄清。
Here is my HTML for clarification.
<input id="foreign_checkbox" type="checkbox" />
<span id="additional_foreign">Click Me</span>
推荐答案
#foreign_checkbox')。is(:checked) - 其余代码看起来不错
try using $('#foreign_checkbox').is(":checked")
- rest of the code looks fine
如果这是我的代码
<input id="foreign_checkbox" type="checkbox" />
<span style='display:none' id="additional_foreign">Click Me</span>
<script type="text/javascript">
$(document).ready(function() {
$("#foreign_checkbox").click(function() {
if($('#foreign_checkbox').is(':checked')) {
$('#additional_foreign').show();
} else {
$('#additional_foreign').hide();
}
});
$('#additional_foreign').click(function() {
alert('This click function works');
});
});
</script>
这篇关于如何检查一个复选框是否选中与JQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文