How do I check whether a checkbox is checked in jQuery?

前端 未结 30 3512
花落未央
花落未央 2020-11-21 04:44

I need to check the checked property of a checkbox and perform an action based on the checked property using jQuery.

For example, if the age checkbox is

30条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-21 05:20

    Using jQuery > 1.6

    
    
    // traditional attr
    $('#checkMeOut').attr('checked'); // "checked"
    // new property method
    $('#checkMeOut').prop('checked'); // true
    

    Using the new property method:

    if($('#checkMeOut').prop('checked')) {
        // something when checked
    } else {
        // something else when not
    }
    

提交回复
热议问题