Detect when input has a 'readonly' attribute

后端 未结 8 1205
感动是毒
感动是毒 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条回答
  •  猫巷女王i
    2021-02-05 00:01

    Check the current value of your "readonly" attribute, if it's "false" (a string) or empty (undefined or "") then it's not readonly.

    $('input').each(function() {
        var readonly = $(this).attr("readonly");
        if(readonly && readonly.toLowerCase()!=='false') { // this is readonly
            alert('this is a read only field');
        }
    });
    

提交回复
热议问题