javascript - 如何在输入中只写一个符号?
问题描述
我想将输入字段限制为一个字母(使用 JS),在一个字母之后我想得到一个错误。怎么做?
解决方案
你会想要这样的东西
香草JS
<input onkeydown="checkLen(this)" type="text" maxlength="1" />
function checkLen(e) {
if (el.value.length > 1) {
alert("ERROR");
}
}
jQuery
$('#input').keydown( function(e){
if ($(this).val().length > 1) {
alert('ERROR');
}
});
推荐阅读
- r - 在 R 中查找正则表达式调用以仅返回名字和姓氏或完整的团队名称
- java - 如何在多逗号行中使用逗号分隔字符串
- node.js - 将节点版本 8 更新到 10 时如何获得所有兼容的软件包版本?
- ios - 我们可以为 iPhone 和 Apple Watch 应用程序使用相同的代码库吗?
- azure - 在 Log Analytics 中使用共享的用户定义 Kusto 函数
- rust - 在 FFI 中使用 () (和其他零大小类型)
- wai-aria - Aria-live="polite" 在 NVDA 屏幕阅读器上的阅读顺序不正确?
- google-app-engine - GKE 上用于出口到 GAE 应用程序的 IP 地址是 0.0.0.0,而不是带有 Cloud NAT 的 IPv4
- ansible - aws_s3 - 不确定我是否了解如何使用
- deep-learning - 将预训练的深度学习模型用于商业用途