如何使文本输入不可编辑?

发布于 2024-09-18 13:28:18 字数 367 浏览 6 评论 0原文

所以我有一个文本输入

<input type="text" value="3" class="field left">

这是我的 CSS 是否

background:url("images/number-bg.png") no-repeat scroll 0 0 transparent;
border:0 none;
color:#FFFFFF;
height:17px;
margin:0 13px 0 0;
text-align:center;
width:17px; 

有一个设置或技巧,我正在考虑做一个标签而不是样式。我如何转换它们,有更好的方法还是唯一的方法?

So I have a text input

<input type="text" value="3" class="field left">

Here is my CSS for it

background:url("images/number-bg.png") no-repeat scroll 0 0 transparent;
border:0 none;
color:#FFFFFF;
height:17px;
margin:0 13px 0 0;
text-align:center;
width:17px; 

Is there a setting or a trick to this, I was thinking of doing a label instead but how about the styling. How do I convert them and is there a better way or is that the only way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

三生殊途 2024-09-25 13:28:18
<input type="text" value="3" class="field left" readonly>

无需造型。

请参阅 MDN 上的 https ://developer.mozilla.org/en/docs/Web/HTML/Element/input#Attributes

<input type="text" value="3" class="field left" readonly>

No styling necessary.

See <input> on MDN https://developer.mozilla.org/en/docs/Web/HTML/Element/input#Attributes

白日梦 2024-09-25 13:28:18

您可以将属性 readonly 添加到输入:

<input type="text" value="3"
       class="field left" readonly="readonly">

更多信息:
http://www.w3schools.com/tags/att_input_readonly.asp

You can add the attribute readonly to the input:

<input type="text" value="3"
       class="field left" readonly="readonly">

More info:
http://www.w3schools.com/tags/att_input_readonly.asp

别在捏我脸啦 2024-09-25 13:28:18

如果您希望仅读取输入,则可以使用 readonly 属性。如果您希望显示输入但完全禁用(即使像 PHP 这样的处理语言也无法读取这些内容),您可以使用 disabled 属性。

You can use readonly attribute, if you want your input only to be read. And you can use disabled attribute, if you want input to be shown, but totally disabled (even processing languages like PHP wont be able to read those).

暮年 2024-09-25 13:28:18

只是为了完成可用的答案:

输入元素可以是只读的或禁用的(它们都不可编辑,但有一些区别:焦点,...)

可以在这里找到很好的解释:
HTML表单输入字段的disabled=“disabled”和readonly=“readonly”有什么区别?

使用方法:

<input type="text" value="Example" disabled /> 
<input type="text" value="Example" readonly />

有还有一些通过 CSS 或 JavaScript 实现的解决方案,如此处所述。

Just to complete the answers available:

An input element can be either readonly or disabled (none of them is editable, but there are a couple of differences: focus,...)

Good explanation can be found here:
What's the difference between disabled=“disabled” and readonly=“readonly” for HTML form input fields?

How to use:

<input type="text" value="Example" disabled /> 
<input type="text" value="Example" readonly />

There are also some solutions to make it through CSS or JavaScript as explained here.

带刺的爱情 2024-09-25 13:28:18

如果您确实想使用 CSS,请使用以下属性,这将使字段不可编辑。

pointer-events: none;

if you really want to use CSS, use following property which will make field non-editable.

pointer-events: none;
年华零落成诗 2024-09-25 13:28:18

添加 readonly

<input type="text" value="3" class="field left" readonly>

如果您希望不在表单中提交值,请添加 disabled 属性。

<input type="text" value="3" class="field left" disabled>

没有办法使用总是有效的 CSS 来做到这一点。

为什么? CSS 不能“禁用”任何东西。您仍然可以关闭显示或可见性并使用 pointer-events: none,但 pointer-events 不适用于 IE 11 之前发布的 IE 版本。

Add readonly:

<input type="text" value="3" class="field left" readonly>

If you want the value to be not submitted in a form, instead add the disabled attribute.

<input type="text" value="3" class="field left" disabled>

There is no way to use CSS that always works to do this.

Why? CSS can't "disable" anything. You can still turn off display or visibility and use pointer-events: none but pointer-events doesn't work on versions of IE that came out earlier than IE 11.

北恋 2024-09-25 13:28:18
<input type="text" value="3" class="field left" readonly>

您可以在 https://www.w3schools.com/tags/att_input_readonly.asp 中看到

设置“readonly”的方法:

$("input").attr("readonly", true)

取消“readonly”(在jQuery中工作):

$("input").attr("readonly", false)
<input type="text" value="3" class="field left" readonly>

You could see in https://www.w3schools.com/tags/att_input_readonly.asp

The method to set "readonly":

$("input").attr("readonly", true)

to cancel "readonly"(work in jQuery):

$("input").attr("readonly", false)
深海夜未眠 2024-09-25 13:28:18

你只需要在最后添加 disabled

<input type="text" value="3" class="field left" disabled>

you just need to add disabled at the end

<input type="text" value="3" class="field left" disabled>
喜爱皱眉﹌ 2024-09-25 13:28:18

每个输入都必须有只读或可编辑字段,因此请使用它

every input must have readonly or editable field so use it

浅听莫相离 2024-09-25 13:28:18

readonlydisabled 将起作用。但如果你想发送输入值,只需使用 readonly 并编辑你的 css .field left:focus{ Outline: none }

readonly and disabled will work. But if you want to send input value, just use readonly and edit your css .field left:focus{ outline: none }

暖树树初阳… 2024-09-25 13:28:18

在某些情况下,您只想使输入元素不可编辑或不可更改,这意味着文本光标仍然可以放置在那里,并且光标可以四处移动,但只有文本保持不变。它与只读或禁用不同。

  1. 纯JS(jsfiddle)
document.getElementById('input')
  .addEventListener('keydown', (e) => {
  var field = e.target;
  var oldValue = field.value;
  setTimeout(function () {
    if(field.value !== oldValue) {
      field.value = oldValue;
    } 
  }, 1);
  })
<input id="input", type="text" value="default" class="field left">

  1. 使用 JQuery(jsfiddle)
$("input").keydown(function(e) {
var oldValue=$(this).val();
var field=this;
setTimeout(function () {
    if(field.value !== oldValue) {
        $(field).val(oldValue);
    } 
}, 1);
});
<input id="input", type="text" value="default" class="field left">

In some cases, you just want to make the input element non-editable or unchangeable, which means the text cursor can still be placed there and the cursor can be moved around but only the text remains unchanged. It differs from readOnly or disabled.

  1. Pure JS(jsfiddle)

document.getElementById('input')
  .addEventListener('keydown', (e) => {
  var field = e.target;
  var oldValue = field.value;
  setTimeout(function () {
    if(field.value !== oldValue) {
      field.value = oldValue;
    } 
  }, 1);
  })
<input id="input", type="text" value="default" class="field left">

  1. use JQuery(jsfiddle)
$("input").keydown(function(e) {
var oldValue=$(this).val();
var field=this;
setTimeout(function () {
    if(field.value !== oldValue) {
        $(field).val(oldValue);
    } 
}, 1);
});
<input id="input", type="text" value="default" class="field left">
笑红尘 2024-09-25 13:28:18

如果您正在寻找 React 那么您可以尝试:

<input type="text" value="John" readOnly={true}/>

In case you are looking for React then you may try:

<input type="text" value="John" readOnly={true}/>
缘字诀 2024-09-25 13:28:18

您也可以使用 JavaScript 来完成:

<input id="my-input" type="text" value="3" />

<script>
  document.querySelector("#my-input").readOnly = false;
</script>

You can also do it with javascript:

<input id="my-input" type="text" value="3" />

<script>
  document.querySelector("#my-input").readOnly = false;
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文