javascript - 禁用所有键,但复制粘贴组合
问题描述
我正在为数字字段编写代码,其中我禁用了除数字键之外的所有键
function doValidation(event) {
var charCode = event.keyCode;
if (charCode != 190 && charCode != 40 && charCode != 39 && charCode != 38 && charCode != 37 && charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 96 || charCode > 105))
return false;
}
<input type="text" onkeydown="doValidation(event)">
现在我想在这个函数中启用 ctrl+c 和 ctrl+v。
解决方案
希望这会有所帮助。
$('input[type="number"]').keypress(function(e){
//Numbers 47 to 57 are the key code of digit 0 to 9.
if (![48,49,50,51,52,53,54,55,56,57].includes(e.keyCode)){
e.preventDefault();
}
});
推荐阅读
- android - 使用Moshi和retrofit2解析具有相同键但不同值的json字段
- reactjs - 在相同组件中识别单击的组件
- javascript - 将 CommonJS 默认导出导入为命名导出/无法加载 ES 模块
- python - 如何在 C# 中从 python 脚本异步读取数据
- ssh - 通过端口转发连接使用 ssh
- google-cloud-platform - 在没有角色/所有者的 GCP 上部署 Kubeflow 1.3
- javascript - ava 抛出 npm ERR!当 sinon 存根抛出新错误时测试失败
- java - java - 如何在Java中的Android Studio中比较具有相同JSON中相同键的JSON对象?
- r - 如何将此数据框写入excel(xlsx)?
- javascript - 合并2个html5画布的捕获视频流