checkbox is always "on"

26,455

Solution 1

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

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

Solution 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

Solution 4

Try this

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

Solution 5

Change:

$('#checkbox').val()

to

$('#checkbox').attr('checked')
Share:
26,455
Thiago
Author by

Thiago

Updated on July 09, 2022

Comments

  • Thiago
    Thiago almost 2 years

    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.

  • Thiago
    Thiago almost 13 years
    I'm getting always "false". I don't know why.
  • Thiago
    Thiago almost 13 years
    I'm getting always "false" using this to.
  • kd7iwp
    kd7iwp about 12 years
    I like how this method gives you a boolean.
  • Derek Litz
    Derek Litz over 10 years
    The on semantics are very confusing... I hope it made someone's life easier! Even a disabled checkbox has a value of "on"...
  • Daniel Howard
    Daniel Howard about 10 years
    Very confusing implementation - but this answer helped me understand why val() always returns 'on' for checkboxes.
  • Gabriel Petrovay
    Gabriel Petrovay about 9 years
    wtf @chx101! what do you want from me? :P :D
  • n1ru4l
    n1ru4l over 7 years
    Vanilla JS: var isChecked = elDom.checked // _> true/false