输入字段,只有数字jquery / js [英] input field, only numbers jquery/js

查看:69
本文介绍了输入字段,只有数字jquery / js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入字段,我只希望用户输入数字

I have a input field where i only wish users to type numbers

html:< input id =numtype = textname =pagesize =4value =/>

jquery / js:

jquery/ js:

 $("#num").keypress(function (e){
      if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){
        return false;
      }
});

希望有人可以帮助我。

hope someone can help me.

btw:我对一个更大的jquery插件感兴趣,以使该功能正常工作。 (我找到了一些jquery-plugins,但必须有其他方法来修复它,使用较小的代码)

btw: I'm not interesting in a larger jquery plugin to make the function work. (I have found some jquery-plugins , but there must be som other ways to fix it, with a smaller code)

推荐答案

试试这个:

$("#num").keypress(function (e){
  var charCode = (e.which) ? e.which : e.keyCode;
  if (charCode > 31 && (charCode < 48 || charCode > 57)) {
    return false;
  }
});

值48到57代表数字0-9。

Values 48 through 57 represent the digits 0-9.

这篇关于输入字段,只有数字jquery / js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆