检测input是否具有“只读”属性
我想在input具有“只读”属性时发出警报。 我试过这个:
if($('input').attr('readonly') == 'readonly'){ alert("foo"); } 我认为“如果”甚至不是最好的办法。
 最快的方法是使用.is() jQuery函数。 
 if ( $('input').is('[readonly]') ) { } 
 使用[readonly]作为select器只是检查属性是否定义在你的元素上。 如果你想检查一个值,你可以使用这样的代替: 
 if ( $('input').is('[readonly="somevalue"]') ) { } 
自从JQuery 1.6以来,总是使用.prop()阅读为什么在这里: http ://api.jquery.com/prop/
 if($('input').prop('readonly')){ } 
.prop()也可以用来设置属性
 $('input').prop('readonly',true); $('input').prop('readonly',false); 
您可以只使用属性select器,然后testing长度:
 $('input[readonly]').length == 0 // --> ok $('input[readonly]').length > 0 // --> not ok 
检查你的“只读”属性的当前值,如果它是“假”(一个string)或空(未定义或“”),那么它不是只读的。
 $('input').each(function() { var readonly = $(this).attr("readonly"); if(readonly && readonly.toLowerCase()!=='false') { // this is readonly alert('this is a read only field'); } }); 
尝试一个简单的方法:
 if($('input[readonly="readonly"]')){ alert("foo"); } 
尝试这个:
 if($('input').attr('readonly') == undefined){ alert("foo"); } 
 如果不存在,它将在js中undefined