如何禁用输入类型=文本? [英] How to disable an input type=text?

查看:70
本文介绍了如何禁用输入类型=文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能,我想使用JavaScript禁止在 text 类型的输入字段中写入。
输入字段从数据库填充;这就是为什么我不希望用户修改它的值。

I want to disable writing in an input field of type text using JavaScript, if possible. The input field is populated from a database; that is why I don't want the user to modify its value.

推荐答案

如果您在页面呈现时知道这一点,这听起来像你所做的,因为数据库有一个值,最好是在呈现而不是JavaScript时禁用它。为此,只需添加 readonly 属性(或 已禁用 ,如果你想从表单提交中移除它)到< input> ,像这样:

If you know this when the page is rendered, which it sounds like you do because the database has a value, it's better to disable it when rendered instead of JavaScript. To do that, just add the readonly attribute (or disabled, if you want to remove it from the form submission as well) to the <input>, like this:

<input type="text" disabled="disabled" />
//or...
<input type="text" readonly="readonly" />

这篇关于如何禁用输入类型=文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆