如何使用jquery查找只读文本字段

时间:2011-11-03 12:18:22

标签: javascript jquery

我怎样才能在表单中找到只读文本字段..我试过这个......但是它不起作用..

$('input[readonly="readonly"]').each(function() 
{
$(this).attr('tabindex', '-1');
}

5 个答案:

答案 0 :(得分:3)

它适用于我,两者:

console.log($('input[readonly="readonly"]'));

console.log($('input[readonly]'))

请参阅此fiddle


编辑:

您的代码存在的问题是缺少结尾)。见working fiddle

$('input[readonly="readonly"]').each(function() 
{
    $(this).attr('tabindex', '-1');
});

关于动态设置readonly然后搜索输入的评论问题:

$('input[name="country"]').attr('readonly', 'readonly');

$('input[readonly="readonly"]').each(function()
{
    $(this).attr('tabindex', '-1');
});

See it working here

答案 1 :(得分:0)

你可以这样做:

$('input').filter(function() { return !!$(this).prop('readonly'); }).each(function() {
  // ...
});

您还可以检查“.each()”回调函数体中的属性:

$('input').each(function() {
  if (!$(this).prop('readonly')) return;
  // ... 
});

答案 2 :(得分:0)

尝试

$('input[readonly]').each(function() 
{
    $(this).attr('tabindex', '-1');
}

可能是因为并非每个浏览器都只读取值,但没有任何内容。

答案 3 :(得分:0)

http://jsfiddle.net/8aRg8/

jQuery(function($){
    $('input').each(function() {
        if ($(this).is('[readonly]')) alert(this.id);
    });
});

答案 4 :(得分:0)

将其用作选择器应该足够了:input[readonly]

只要具有属性,至少chrome会将输入视为只读,无论值是什么。