checkbox is always “on”

♀尐吖头ヾ 提交于 2019-12-10 00:36:54

问题


Hy!

When Uploadify send the file to the action, I need to know if the checbox is checked or not, so I did:

    $('#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.

can anyone help? 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".

How can I send a form with uploadify?

Tks.


回答1:


try $('#checkbox').is(':checked')




回答2:


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.

The HTML Form implements the following semantics:

if ($('#mycheckbox').is(':checked')) {
    payload['mycheckbox'] = $('#mycheckbox').val();
}

You can either mimic this semantics in Ajax or send the 'checked' flag directly as follows:

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:

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.




回答3:


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:

$('#checkbox').prop('checked'); // -> true/false
$('#checkbox').prop('disabled'); // -> true/false



回答4:


Try this

$('#checkbox').is(':checked')



回答5:


Change:

$('#checkbox').val()

to

$('#checkbox').attr('checked')



回答6:


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');
}



回答7:


Use:

$('#checkbox').attr('checked')



回答8:


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..

Thanks



来源:https://stackoverflow.com/questions/6602115/checkbox-is-always-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!