检测输入何时具有“只读"属性 [英] Detect when input has a 'readonly' attribute

查看:66
本文介绍了检测输入何时具有“只读"属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当输入具有只读"属性时,我想发出警报.我已经尝试过了:

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

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

    alert("foo");
  }

我认为"if"甚至不是最好的方法.

I think that 'if' is not even the best way to do it.

推荐答案

最快的方法是使用.is() jQuery函数.

fastest way is to use the .is() jQuery function.

if ( $('input').is('[readonly]') ) { }

使用[readonly]作为选择器仅检查属性是否在元素上定义.如果您想检查一个值,则可以改用这样的方法:

using [readonly] as a selector simply checks if the attribute is defined on your element. if you want to check for a value, you can use something like this instead:

if ( $('input').is('[readonly="somevalue"]') ) { }

这篇关于检测输入何时具有“只读"属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆