How I can add and remove the attribute “readonly”?

前端 未结 4 1284
灰色年华
灰色年华 2021-02-18 20:19
$(document).ready(function() {
  //Check City Value
  var city_value = parseInt($(\"#city\").val());
  if( city_value == 0) {
     $(\"#state\").attr(\"readonly\", true)         


        
4条回答
  •  遥遥无期
    2021-02-18 20:58

    You can do it in pure JavaScript (or Vanilla JS) without jQuery:

    Set attribute readOnly:

    document.getElementById("state").readOnly = true;
    

    and unset:

    document.getElementById("state").readOnly = false;
    

    Equivalent to object-oriented notation is also this array-oriented notation:

    document.getElementById("state")['readOnly']
    

    It is also important that in JavaScript all the properties are case sensitive, so it is important to remember about the large O in the readOnly property, otherwise it will not work...

提交回复
热议问题