Setting a control to readonly using jquery 1.6 .prop()

后端 未结 2 682
不思量自难忘°
不思量自难忘° 2020-12-18 18:11

With the release of jQuery 1.6, the recommendation on SO has been to generally start using prop() where you used to use attr().

What happens when I want make an elem

2条回答
  •  抹茶落季
    2020-12-18 18:42

    Try this:

    $(".control").prop({ readOnly: true });
    

    I think of it like this: .attr() gets the default value in the html markup while .prop() gets/sets the value dynamically. Look at the following:

    
    
    $(".control").attr("readOnly") // would yield "readOnly"
    $(".control").prop("readOnly") // would yield true
    $(".control").is(":readOnly")  // would yield true
    

    The api documentation says this:

    The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() only retrieves attributes.

提交回复
热议问题