Detect when input has a 'readonly' attribute

后端 未结 8 1173
感动是毒
感动是毒 2021-02-04 23:12

I want to throw an alert when an input has a \'readonly\' attribute. I have tried this:

  if($(\'input\').attr(\'readonly\') == \'readonly\'){

    alert(\"foo\"         


        
8条回答
  •  孤城傲影
    2021-02-05 00:04

    Since JQuery 1.6, always use .prop() Read why here: http://api.jquery.com/prop/

    if($('input').prop('readonly')){ }
    

    .prop() can also be used to set the property

    $('input').prop('readonly',true);
    
    $('input').prop('readonly',false);
    

提交回复
热议问题