checkbox is always “on”

后端 未结 8 1291
抹茶落季
抹茶落季 2021-01-07 16:32

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({
        \         


        
相关标签:
8条回答
  • 2021-01-07 16:50

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

    0 讨论(0)
  • 2021-01-07 16:52

    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
    
    0 讨论(0)
  • 2021-01-07 16:53

    Try this

    $('#checkbox').is(':checked')
    
    0 讨论(0)
  • 2021-01-07 16:56

    Use:

    $('#checkbox').attr('checked')
    
    0 讨论(0)
  • 2021-01-07 17:00

    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');
    }
    
    0 讨论(0)
  • 2021-01-07 17:03

    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.

    0 讨论(0)
提交回复
热议问题