Jquery if checkbox is checked Bootstrap switch

后端 未结 6 1639
情话喂你
情话喂你 2021-01-18 23:13

I use Bootstrap switch plugin to make my checkboxes looks like switch buttons. I need to check if the checkbox is checked in jQuery. I Googled a lot and I tried some advice

6条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 23:44

    Demo

    Use .prop() instead of .attr().

    • .prop returns - 'true' or 'false'. Use this
    • .is(':checked') returns - 'true' or 'false' -:checked is a pseudo slector. Or this
    • .attr returns - 'checked' or 'attribute undefined'.

    $('#uho').prop('checked')
    
    if($('#uho').prop('checked')){
        $.cookie("typpaliva", "Diesel");
    }
    else {
        $.cookie("typpaliva", "Benzin");
    }
    

提交回复
热议问题