jQuery 复选框总是“打开”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6602115/
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 is always "on"
提问by Thiago
Hy!
嗨!
When Uploadify send the file to the action, I need to know if the checbox is checked or not, so I did:
当 Uploadify 将文件发送到操作时,我需要知道复选框是否被选中,所以我做了:
$('#uploaded').uploadify({
'uploader': '/uploadify.swf',
'cancelImg': '/cancel.png',
'script': '/Interaction/Upload',
'multi': true,
'auto': false,
'method': 'post',
'scriptData': {'Checkbox': $('#checkbox').val()},
});
But I aways get an "on" value. No matter if is checked or not.
但是我得到了一个“on”值。不管查不查。
can anyone help? Tks.
有人可以帮忙吗?Tks。
UPDATE:
更新:
I realized that uploadify is getting the checkbox when the page is loaded.This means that if I change the checkbox (or any other type of input) the uploadify will get the initial value, in this case, "checkbox = false".
我意识到当页面加载时uploadify 正在获取复选框。这意味着如果我更改复选框(或任何其他类型的输入),uploadify 将获得初始值,在这种情况下,“checkbox = false”。
How can I send a form with uploadify?
如何使用uploadify发送表单?
Tks.
Tks。
回答by MikeM
try $('#checkbox').is(':checked')
尝试 $('#checkbox').is(':checked')
回答by Gopi Reddy
A checkbox's value is always 'on'. The HTML Form sends the value to the server only if the checkbox is checked. If the box is not checked, then the request parameter will be absent. This semantics is apparently to minimize the differences between checkboxes and radio buttons.
复选框的值始终为“on”。HTML表单才会在选中复选框时将值发送到服务器。如果未选中该框,则请求参数将不存在。这种语义显然是为了最小化复选框和单选按钮之间的差异。
The HTML Form implements the following semantics:
HTML 表单实现以下语义:
if ($('#mycheckbox').is(':checked')) {
payload['mycheckbox'] = $('#mycheckbox').val();
}
You can either mimic this semantics in Ajax or send the 'checked' flag directly as follows:
您可以在 Ajax 中模仿这种语义,也可以直接发送“已检查”标志,如下所示:
payload['mycheckbox'] = $('#mycheckbox').is(':checked');
I'd recommend mimic'ing the HTML Form semantics. Irrespective of what the client does, I'd recommend the following in the server code:
我建议模仿 HTML 表单语义。无论客户端做什么,我都建议在服务器代码中使用以下内容:
if mycheckbox param exists and its value is either 'true' or 'on' { // pseudo code of course
// mycheckbox is checked
} else {
// mycheckbox is unchecked
}
Hope this explanation helps.
希望这个解释有帮助。
回答by Gabriel Petrovay
There is a pretty handy way to check all these element properties like disabled
, checked
, etc. with the prop
function. This also converts it to boolean:
还有就是要检查像所有这些元素的属性相当方便的方法disabled
,checked
等用的prop
功能。这也将其转换为布尔值:
$('#checkbox').prop('checked'); // -> true/false
$('#checkbox').prop('disabled'); // -> true/false
回答by ShankarSangoli
Try this
尝试这个
$('#checkbox').is(':checked')
回答by rossipedia
Change:
改变:
$('#checkbox').val()
to
到
$('#checkbox').attr('checked')
回答by nimrod
I just had the same problem and found this solution:
我刚刚遇到了同样的问题并找到了这个解决方案:
if($("input[name='yourcheckbox']").is(':checked')) {
console.log('chb checked');
} else {
console.log('chb not checked');
}
回答by Mark Pope
Use:
用:
$('#checkbox').attr('checked')
回答by Javascript Coder
You can give
你可以给
if(jQuery(this).attr('checked')){ // u have to use this selector according to your function it may be .click .live or else..
if(jQuery(this).attr('checked')){ // 你必须根据你的功能使用这个选择器它可能是 .click .live 或者其他..
Thanks
谢谢