如何使文本输入不可编辑?
所以我有一个文本输入
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
无需造型。
请参阅 MDN 上的
https ://developer.mozilla.org/en/docs/Web/HTML/Element/input#Attributes
No styling necessary.
See
<input>
on MDN https://developer.mozilla.org/en/docs/Web/HTML/Element/input#Attributes您可以将属性
readonly
添加到输入:更多信息:
http://www.w3schools.com/tags/att_input_readonly.asp
You can add the attribute
readonly
to the input:More info:
http://www.w3schools.com/tags/att_input_readonly.asp
如果您希望仅读取输入,则可以使用
readonly
属性。如果您希望显示输入但完全禁用(即使像 PHP 这样的处理语言也无法读取这些内容),您可以使用disabled
属性。You can use
readonly
attribute, if you want your input only to be read. And you can usedisabled
attribute, if you want input to be shown, but totally disabled (even processing languages like PHP wont be able to read those).只是为了完成可用的答案:
输入元素可以是只读的或禁用的(它们都不可编辑,但有一些区别:焦点,...)
可以在这里找到很好的解释:
HTML表单输入字段的disabled=“disabled”和readonly=“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:
There are also some solutions to make it through CSS or JavaScript as explained here.
如果您确实想使用 CSS,请使用以下属性,这将使字段不可编辑。
if you really want to use CSS, use following property which will make field non-editable.
添加
readonly
:如果您希望不在表单中提交值,请添加
disabled
属性。没有办法使用总是有效的 CSS 来做到这一点。
为什么? CSS 不能“禁用”任何东西。您仍然可以关闭显示或可见性并使用
pointer-events: none
,但pointer-events
不适用于 IE 11 之前发布的 IE 版本。Add
readonly
:If you want the value to be not submitted in a form, instead add the
disabled
attribute.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
butpointer-events
doesn't work on versions of IE that came out earlier than IE 11.您可以在 https://www.w3schools.com/tags/att_input_readonly.asp 中看到
设置“readonly”的方法:
取消“readonly”(在jQuery中工作):
You could see in https://www.w3schools.com/tags/att_input_readonly.asp
The method to set "readonly":
to cancel "readonly"(work in jQuery):
你只需要在最后添加 disabled
you just need to add disabled at the end
每个输入都必须有只读或可编辑字段,因此请使用它
every input must have readonly or editable field so use it
readonly
和disabled
将起作用。但如果你想发送输入值,只需使用readonly
并编辑你的 css .field left:focus{ Outline: none }readonly
anddisabled
will work. But if you want to send input value, just usereadonly
and edit your css .field left:focus{ outline: none }在某些情况下,您只想使输入元素不可编辑或不可更改,这意味着文本光标仍然可以放置在那里,并且光标可以四处移动,但只有文本保持不变。它与只读或禁用不同。
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.
如果您正在寻找 React 那么您可以尝试:
In case you are looking for React then you may try:
您也可以使用 JavaScript 来完成:
You can also do it with javascript: