Detect when input has a 'readonly' attribute

后端 未结 8 1194
感动是毒
感动是毒 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:07

    Try a simple way:

    if($('input[readonly="readonly"]')){
       alert("foo");
    }
    
    0 讨论(0)
  • 2021-02-05 00:08

    You can just use the attribute selector and then test the length:

    $('input[readonly]').length == 0 // --> ok
    $('input[readonly]').length > 0  // --> not ok
    
    0 讨论(0)
提交回复
热议问题