How can I use jQuery to make an input readonly?

后端 未结 13 2164
挽巷
挽巷 2020-12-07 09:54

I have the following input:

  

How c

13条回答
  •  一整个雨季
    2020-12-07 10:28

    simply add the following attribute

    // for disabled i.e. cannot highlight value or change
    disabled="disabled"
    
    // for readonly i.e. can highlight value but not change
    readonly="readonly"
    

    jQuery to make the change to the element (substitute disabled for readonly in the following for setting readonly attribute).

    $('#fieldName').attr("disabled","disabled") 
    

    or

    $('#fieldName').attr("disabled", true) 
    

    NOTE: As of jQuery 1.6, it is recommended to use .prop() instead of .attr(). The above code will work exactly the same except substitute .attr() for .prop().

提交回复
热议问题