Js通过键盘控制键盘按下和松开触发事件

输入框标签:onkeydown="num_checked()" id="num" />

Js:

//只能输入数字
 function num_checked(){
  //48-57 96-105(数字键盘) 8 9(8为回格键,9为Tab键)
  var code = event.keyCode;
  if(code>=48&&code<=57||code>=96&&code<=105||code==8||code==9){
  event.returnValue= true;
  }else{
  event.returnValue= false;
         }
  }

function addAmounts_work(el){
        var amount1 = el.value;
  var amount2 = $("#workCompensateAmount").val();
  var amounts;
  amounts = Number(amount1)+Number(amount2);
  amounts = Number(amounts.toFixed(2));//保留2位小数
  $("#compensationAmount").val(amounts);
 }

html:


你可能感兴趣的:(Js,keyCode)