如何使用jQuery将输入设为只读?


142

我有以下输入:

  <input id="fieldName" name="fieldName" type="text" class="text_box" value="Firstname"/>

如何在不更改元素或其值的情况下使用jQuery使该元素成为只读输入?

Answers:


255

如今,对于jQuery 1.6.1或更高版本,建议在设置布尔属性/属性时使用.prop()。

$("#fieldName").prop("readonly", true);

89

只需添加以下属性

// for disabled i.e. cannot highlight value or change
disabled="disabled"

// for readonly i.e. can highlight value but not change
readonly="readonly"

jQuery来进行更改到元件(替代disabledreadonly在下文中用于设置readonly属性)。

$('#fieldName').attr("disabled","disabled") 

要么

$('#fieldName').attr("disabled", true) 

注意:从jQuery 1.6开始,建议使用.prop()代替.attr()。上面的代码将工作完全一样,除了替代.attr().prop()


4
禁用字段与使其不可编辑是不同的,对吧?禁用它也将使其不提交
Dave 2015年

2
@戴夫正确。只读输入也可以被聚焦,禁用输入不能被聚焦
Russ Cam

74

要将输入设为只读,请使用:

 $("#fieldName").attr('readonly','readonly');

使它读/写使用:

$("#fieldName").removeAttr('readonly');

添加disabled属性不会通过post发送值。


7

您可以简单地将其标记为disabledenabled。您可以使用以下代码执行此操作:

//for disable
$('#fieldName').prop('disabled', true);

//for enable 
$('#fieldName').prop('disabled', false);

要么

$('#fieldName')。prop('readonly',true);

$('#fieldName')。prop('readonly',false);

---最好使用prop而不是attr。


7
禁用的输入不会在表单发布/获取中提交。
Nielsvh 2016年

4

使用此示例将文本框设置为“只读”或“只读”。

<input type="textbox" class="txt" id="txt"/>
<input type="button" class="Btn_readOnly" value="Readonly" />
<input type="button" class="Btn_notreadOnly" value="Not Readonly" />

<script>

    $(document).ready(function(){
       ('.Btn_readOnly').click(function(){
           $("#txt").prop("readonly", true);
       });

       ('.Btn_notreadOnly').click(function(){
           $("#txt").prop("readonly", false);
       });
    });

</script>

3

有两个属性,即readonlydisabled,可以进行半只读输入。但是它们之间有微小的区别。

<input type="text" readonly />
<input type="text" disabled />
  • readonly属性使您的输入文本被禁用,并且用户无法再对其进行更改。
  • disabled属性不仅会使您的输入文本被禁用不可更改),而且也无法提交它

jQuery方法(1):

$("#inputID").prop("readonly", true);
$("#inputID").prop("disabled", true);

jQuery方法(2):

$("#inputID").attr("readonly","readonly");
$("#inputID").attr("disabled", "disabled");

JavaScript方法:

document.getElementById("inputID").readOnly = true;
document.getElementById("inputID").disabled = true;

PS prop随附jQuery 1.6


0

鉴于-

<input name="foo" type="text" value="foo" readonly />

这有效-(jquery 1.7.1)

$('input[name="foo"]').prop('readonly', true);

用readonly和readOnly测试-都可以。


-1

也许使用禁用的属性:

<input disabled="disabled" id="fieldName" name="fieldName" type="text" class="text_box" />

或者只是使用标签标签:;)

<label>

1
这个问题说“使用jQuery”(意味着应该动态完成)和“只读”(与禁用不同)。
昆汀2009年

-1
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
    <title></title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

</head>
<body>
    <div>
        <input id="fieldName" name="fieldName" type="text" class="text_box" value="Firstname" />
    </div>
</body>

<script type="text/javascript">
    $(function()
    {
        $('#fieldName').attr('disabled', 'disabled');

    });
</script>
</html>

-1

也许也很有意义

$('#fieldName').prop('readonly',false);

可以用作切换选项。


我尝试使用jQuery 3.3.1进行此操作,并且readonly不管传递的值如何,它只是向元素添加了一个属性。因此,尽管表面上将值设置为false,但您的示例仍将使输入字段为只读。
TMN

-1

在JQuery 1.12.1中,我的应用程序使用以下代码:

$('"#raisepay_id"')[0].readOnly=true;

$('"#raisepay_id"')[0].readOnly=false;

而且有效。


-2

setReadOnly(state)对于表单非常有用,我们可以直接或根据各种条件将任何字段设置为setReadOnly(state)。但是我更喜欢使用readOnly为选择器设置不透明度,否则attr ='disabled'也可以像同样的方法。

readOnly示例:

$('input').setReadOnly(true);

或通过像

var same = this.checked;
$('input').setReadOnly(same);

在这里,我们使用状态布尔值来设置和删除输入的只读属性,具体取决于复选框的单击。


上面的示例不起作用-setReadOnly不是内置在jquery或任何浏览器中的函数
Dave B

-3

在html中

$('#raisepay_id').attr("readonly", true) 

$("#raisepay_id").prop("readonly",true);

在引导中

$('#raisepay_id').attr("disabled", true) 

$("#raisepay_id").prop("disabled",true);

JQuery是一个不断变化的库,有时它们会定期进行改进。.attr()用于从HTML标记中获取属性,尽管它功能完善,但后来又添加了.prop()使其具有更高的语义,并且与诸如“ checked”和“ selected”之类的无值属性更好地配合使用。

建议您使用的是更高版本的JQuery,则应尽可能使用.prop()。


2
我将其否决,因为这是荒谬的。所有这些都是HTML,JavaScript和Bootstrap使用jQuery的,所以事实甚至都不相关。另外,禁用和只读是两种不同的功能,它们与是否使用Bootstrap无关。
simontemplar 2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.