首页 > 解决方案 > 禁用所有键,但复制粘贴组合

问题描述

我正在为数字字段编写代码,其中我禁用了除数字键之外的所有键

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。

标签: javascriptjqueryhtml

解决方案


希望这会有所帮助。

$('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();
  }
});

推荐阅读