如何防止用户在不禁用字段的情况下输入文本字段? [英] How to prevent user from typing in text field without disabling the field?

查看:135
本文介绍了如何防止用户在不禁用字段的情况下输入文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过:

I tried:

$('input').keyup(function() {

   $(this).attr('val', '');

});

但在输入字母后略微移除输入的文字。是否有阻止用户完全输入文本而不诉诸禁用文本字段?

but it removes the entered text slightly after a letter is entered. Is there anyway to prevent the user from entering text completely without resorting to disabling the text field?

推荐答案

非Javascript替代方案可以很容易地忽略:你可以使用 readonly 属性而不是 disabled 属性吗?它可以防止编辑输入中的文本,但浏览器对输入的样式进行不同的设置(不太可能将其灰显)
例如< input readonly type =text...>

A non-Javascript alternative that can be easily overlooked: can you use the readonly attribute instead of the disabled attribute? It prevents editing the text in the input, but browsers style the input differently (less likely to "grey it out") e.g. <input readonly type="text" ...>

这篇关于如何防止用户在不禁用字段的情况下输入文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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