Do checkbox inputs only post data if they're checked?

后端 未结 12 773
盖世英雄少女心
盖世英雄少女心 2020-11-22 08:04

Is it standard behaviour for browsers to only send the checkbox input value data if it is checked upon form submission?

And if no value data is supplied, is the defa

12条回答
  •  死守一世寂寞
    2020-11-22 08:41

    Checkboxes are posting value 'on' if and only if the checkbox is checked. Insted of catching checkbox value you can use hidden inputs

    JS:

    var chk = $('input[type="checkbox"]');
        chk.each(function(){
            var v = $(this).attr('checked') == 'checked'?1:0;
            $(this).after('');
        });
    
    chk.change(function(){ 
            var v = $(this).is(':checked')?1:0;
            $(this).next('input[type="hidden"]').val(v);
        });
    

    HTML:

    
    

提交回复
热议问题