code-porter-233

导航

简单实现输入框 <input type="number" /> 只能输入正整数

<style type="text/css">
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
}
</style>

...

<input type="number" placeholder="请输入正整数" onKeypress="inputCheck(event)" />

...

function inputCheck(e) {

  // 通过判断输入的charCode来过滤掉小数点和减号

  if (e.charCode == 45 || e.charCode == 46 || e.charCode == 69 || e.charCode == 101) {
    e.returnValue = false;
  }
}

posted on 2022-03-16 18:20  瞬间空白  阅读(1011)  评论(0编辑  收藏  举报