JavaScript this.checked

前端 未结 4 1503
你的背包
你的背包 2021-01-05 09:45

In JavaScript, if we write the following for example:

var c = this.checked;

What is checked here? Is it just a state

相关标签:
4条回答
  • 2021-01-05 10:09

    Assuming this refers to a DOM element which has a checked property (e.g. a checkbox or a radio button) then the checked property will either be true if the element is checked, or false if it's not. For example, given this HTML:

    <input type="checkbox" id="example">
    

    The following line of JS will return false:

    var c = document.getElementById("example").checked; //False
    

    Note that what you've written is standard JavaScript, not jQuery. If this refers to a jQuery object rather than a DOM element, checked will be undefined because the jQuery object does not have a checked property. If this is a jQuery object, you can use .prop:

    var c = this.prop("checked");
    
    0 讨论(0)
  • 2021-01-05 10:17

    To use the pseudo selector :checked with the jquery object this write:

    $(this).is(':checked')
    
    0 讨论(0)
  • 2021-01-05 10:30

    In jQuery checked is a selector:

    The :checked selector works for checkboxes and radio buttons.

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

    For Example:

    $('#checkBox').attr('checked'); 
    
    or 
    
    $('#checkBox').is(':checked'); 
    
    0 讨论(0)
  • 2021-01-05 10:31

    Is it just a state that tells us if a checkbox for example is checked or not? So, can we use it to check that the checkbox is also not checked?

    Yes and yes

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