如何使用 JavaScript 将输入字段设为只读?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17825537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:45:58  来源:igfitidea点击:

How to make a input field readonly with JavaScript?

javascriptinputreadonly

提问by chris

I know you can add readonly="readonly"to an input field so its not editable. But I need to use javascript to target the id of the input and make it readonly as I do not have access to the form code (it's generated via marketing software)

我知道您可以添加readonly="readonly"到输入字段,因此它不可编辑。但我需要使用 javascript 来定位输入的 id 并将其设为只读,因为我无权访问表单代码(它是通过营销软件生成的)

I don't want to disable the input as the data should be collected on submit.

我不想禁用输入,因为应该在提交时收集数据。

Here is the page I have added in the below suggestion with no luck so far:

这是我在以下建议中添加的页面,到目前为止没有运气:

https://www.pages05.net/engagedigital/inputReadOnly/test?spMailingID=6608614&spUserID=MTI5MDk4NjkzMTMS1&spJobID=Nzk4MTY3MDMS1&spReportId=Nzk4MTY3MDMS1

https://www.pages05.net/engagedigital/inputReadOnly/test?spMailingID=6608614&spUserID=MTI5MDk4NjkzMTMS1&spJobID=Nzk4MTY3MDMS1&spReportId=Nzk4MTY3MDMS1

Make sure you use <body onload="onLoadBody();">for anyone using this in the future.

确保您<body onload="onLoadBody();">为将来使用它的任何人使用。

回答by Vijay

You can get the input element and then set its readOnlyproperty to trueas follows:

您可以获取输入元素,然后将其readOnly属性设置true为如下:

document.getElementById('InputFieldID').readOnly = true;

Specifically, this is what you want:

具体来说,这就是你想要的:

<script type="text/javascript">
  function onLoadBody() {
    document.getElementById('control_EMAIL').readOnly = true;
  } 
</script>

Call this onLoadBody()function on body tag like:

onLoadBody()在 body 标签上调用这个函数,如:

<body onload="onLoadBody">

View Demo: jsfiddle.

查看演示:jsfiddle

回答by Charlie Haley

The above answers did not work for me. The below does: document.getElementById("input_field_id").setAttribute("readonly", true);

以上答案对我不起作用。以下是: document.getElementById("input_field_id").setAttribute("readonly", true);

And to remove the readonly attribute: document.getElementById("input_field_id").removeAttribute("readonly");

并删除 readonly 属性: document.getElementById("input_field_id").removeAttribute("readonly");

And for running when the page is loaded, it is worth referring to here.

而对于页面加载时的运行,这里值得参考。

回答by Mumthezir VP

document.getElementById('TextBoxID').readOnly = true;    //to enable readonly


document.getElementById('TextBoxID').readOnly = false;   //to  disable readonly

回答by gezzuzz

document.getElementById("").readOnly = true

回答by Sandiip Patil

Try This :

试试这个 :

document.getElementById(<element_ID>).readOnly=true;

回答by Liam

I think you just have readonly="readonly"

我想你只是 readonly="readonly"

<html><body><form><input type="password" placeholder="password" valid="123" readonly=" readonly"></input>

<html><body><form><input type="password" placeholder="password" valid="123" readonly=" readonly"></input>

回答by bobofrut

Here you have example how to set the readonly attribute:

这里有如何设置只读属性的示例:

<form action="demo_form.asp">
  Country: <input type="text" name="country" value="Norway" readonly><br>
  <input type="submit" value="Submit">
</form>