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

前端 未结 30 3519
花落未央
花落未央 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 04:58

    This example is for button.

    Try the following:

    <input type="button" class="check" id="checkall" value="Check All" />  &nbsp; <input type="button" id="remove" value="Delete" /> <br/>
    
    <input type="checkbox" class="cb-element"  value="1" /> Checkbox  1 <br/>
    <input type="checkbox" class="cb-element"  value="2" /> Checkbox  2 <br/>
    <input type="checkbox" class="cb-element"  value="3" /> Checkbox  3 <br/>
    
    
    $('#remove').attr('disabled', 'disabled'); 
    
    $(document).ready(function() {  
    
        $('.cb-element').click(function() {
    
            if($(this).prop('checked'))
            {
                $('#remove').attr('disabled', false);
            }
            else
            {
                $('#remove').attr('disabled', true);
            }
        });   
    
        $('.check:button').click(function()
    {
        var checked = !$(this).data('checked');
        $('input:checkbox').prop('checked', checked);
        $(this).data('checked', checked);
    
        if(checked == true)
        {
            $(this).val('Uncheck All');
             $('#remove').attr('disabled', false);
        }
    
        else if(checked == false)
        {
            $(this).val('Check All');
            $('#remove').attr('disabled', true);
        }
    });
    });
    
    0 讨论(0)
  • 2020-11-21 05:00

    My way of doing this is:

    if ( $("#checkbox:checked").length ) {       
        alert("checkbox is checked");
    } else {
        alert("checkbox is not checked");
    }
    
    0 讨论(0)
  • 2020-11-21 05:00

    Though you have proposed a JavaScript solution for your problem (displaying a textbox when a checkbox is checked), this problem could be solved just by css. With this approach, your form works for users who have disabled JavaScript.

    Assuming that you have the following HTML:

    <label for="show_textbox">Show Textbox</label>
    <input id="show_textbox" type="checkbox" />
    <input type="text" />
    

    You can use the following CSS to achieve the desired functionality:

     #show_textbox:not(:checked) + input[type=text] {display:none;}
    

    For other scenarios, you may think of appropriate CSS selectors.

    Here is a Fiddle to demonstrate this approach.

    0 讨论(0)
  • 2020-11-21 05:01

    Since jQuery 1.6, the behavior of jQuery.attr() has changed and users are encouraged not to use it to retrieve an element's checked state. Instead, you should use jQuery.prop():

    $("#txtAge").toggle(
        $("#isAgeSelected").prop("checked") // For checked attribute it returns true/false;
                                            // Return value changes with checkbox state
    );
    

    Two other possibilities are:

    $("#txtAge").get(0).checked
    $("#txtAge").is(":checked")
    
    0 讨论(0)
  • 2020-11-21 05:01

    There are many ways to check if a checkbox is checked or not:

    Way to check using jQuery

    if (elem.checked)
    if ($(elem).prop("checked"))
    if ($(elem).is(":checked"))
    if ($(elem).attr('checked'))
    

    Check example or also document:

    • http://api.jquery.com/attr/

    • http://api.jquery.com/prop/

    0 讨论(0)
  • 2020-11-21 05:02

    How do I successfully query the checked property?

    The checked property of a checkbox DOM element will give you the checked state of the element.

    Given your existing code, you could therefore do this:

    if(document.getElementById('isAgeSelected').checked) {
        $("#txtAge").show();
    } else {
        $("#txtAge").hide();
    }
    

    However, there's a much prettier way to do this, using toggle:

    $('#isAgeSelected').click(function() {
        $("#txtAge").toggle(this.checked);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" id="isAgeSelected"/>
    <div id="txtAge" style="display:none">Age is something</div>

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