JavaScript this.checked

前端 未结 4 1586
你的背包
你的背包 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:

    
    

    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");
    

提交回复
热议问题