jQuery 复选框值始终为“开”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18377268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checkbox value is always 'on'
提问by
this is my checkbox
这是我的复选框
HTML
HTML
<label class="checkbox">
<input id="eu_want_team" name="eu_want_team" type="checkbox">
</label>
JQuery
查询
var eu_want_team = $('#eu_want_team').val();
alert(eu_want_team);
Its always displaying ON, is it checked or not. Whats the problem with it?
它总是显示ON,是否检查。它有什么问题?
采纳答案by Yotam Omer
Use .is(':checked')
instead: Working jsFiddle
使用.is(':checked')
来代替:工作的jsfiddle
var eu_want_team = $('#eu_want_team').is(':checked');
alert(eu_want_team);
or as @Itay said in comments you can use jQuery's .prop()
to get the checked property value:
或者正如@Itay 在评论中所说,您可以使用 jQuery.prop()
来获取已检查的属性值:
alert($("#eu_want_team").prop("checked"));
回答by Shadow
<label class="checkbox">
<input id="eu_want_team" name="eu_want_team" type="checkbox" value="somevalue">
</label>
<script>
var ele = document.getElementById("eu_want_team");
if(ele.checked)
alert(ele.value)
</script>
回答by Roy M J
This will work :
这将工作:
if ($('#element').is(":checked")) {
eu_want_team = 1;
} else {
eu_want_team = 0;
}
alert(eu_want_team);
回答by Nathan Srivi
Try this
尝试这个
if ( $('#element').is(':checked')){
alert(element);
}
回答by Kimtho6
i think this is what you want to do
我想这就是你想做的
$("#eu_want_team").click(function(){
alert($(this).is(':checked'));
}
回答by Mark
Have a quick look at this answer for checking if a checkbox is checked.
快速查看此答案以检查是否选中了复选框。
How to check whether a checkbox is checked in jQuery?
But basically you want to do something like below to check its value:
但基本上你想要做类似下面的事情来检查它的价值:
if ($("#element").is(":checked")) {
alert("I'm checked");
}