html5 input整数,HTML5:只有整数的数字inputtypes?

只有HTML才能达到最好的效果

将step属性设置为1 :

这在Chrome中似乎有点bug,所以目前可能不是最好的解决scheme。

更好的解决scheme是使用pattern属性,它使用正则expression式来匹配input:

\d是数字的正则expression式, *表示它接受多于一个的数字。 这里是演示: http : //jsfiddle.net/b8NrE/1/

使用JavaScript的简单方法:

使用此代码,文本域限制的input仅input数字….模式是HTML5中可用的新属性

Patter属性doc

这不仅是为所有浏览器的html5工作正常。 尝试这个

οnkeyup="this.value=this.value.replace(/[^0-9]/g,'');"

模式是不错的,但如果你想限制input到只有type =“text”的数字,你可以使用oninput和正则expression式如下:

我为我而去:)

是的,HTML5呢。 试试这个代码( w3school ):

Quantity (between 1 and 5):

查看最小和最大的参数? 我尝试使用Chrome 19(工作)和Firefox 12(没有工作)。

var valKeyDown; var valKeyUp; function integerOnly(e) { e = e || window.event; var code = e.which || e.keyCode; if (!e.ctrlKey) { var arrIntCodes1 = new Array(96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 8, 9, 116); // 96 TO 105 - 0 TO 9 (Numpad) if (!e.shiftKey) { //48 to 57 - 0 to 9 arrIntCodes1.push(48); //These keys will be allowed only if shift key is NOT pressed arrIntCodes1.push(49); //Because, with shift key (48 to 57) events will print chars like @,#,$,%,^, etc. arrIntCodes1.push(50); arrIntCodes1.push(51); arrIntCodes1.push(52); arrIntCodes1.push(53); arrIntCodes1.push(54); arrIntCodes1.push(55); arrIntCodes1.push(56); arrIntCodes1.push(57); } var arrIntCodes2 = new Array(35, 36, 37, 38, 39, 40, 46); if ($.inArray(e.keyCode, arrIntCodes2) != -1) { arrIntCodes1.push(e.keyCode); } if ($.inArray(code, arrIntCodes1) == -1) { return false; } } return true; } $('.integerOnly').keydown(function (event) { valKeyDown = this.value; return integerOnly(event); }); $('.integerOnly').keyup(function (event) { //This is to protect if user copy-pastes some character value ,.. valKeyUp = this.value; //In that case, pasted text is replaced with old value, if (!new RegExp('^[0-9]*$').test(valKeyUp)) { //which is stored in 'valKeyDown' at keydown event. $(this).val(valKeyDown); //It is not possible to check this inside 'integerOnly' function as, } //one cannot get the text printed by keydown event }); //(that's why, this is checked on keyup) $('.integerOnly').bind('input propertychange', function(e) { //if user copy-pastes some character value using mouse valKeyUp = this.value; if (!new RegExp('^[0-9]*$').test(valKeyUp)) { $(this).val(valKeyDown); } });

设置step="any" 。 工作正常。 参考: http : //blog.isotoma.com/2012/03/html5-input-typenumber-and-decimalsfloats-in-chrome/

我一直在努力铬,并有一些问题,即使我使用HTML属性。 我结束了这个JS代码

$("#element").on("input", function(){ var value = $(this).val(); $(this).val(""); $(this).val(parseInt(value)); return true; });

模式总是可取的限制,尝试oninput和min发生1从1号开始只input数字

从规格

step="any"或正浮点数

指定元素值的粒度。

所以你可以简单地将它设置为1 :

你有没有尝试像这样设置step属性为1

只要把它放在你的input字段中: οnkeypress='return event.charCode >= 48 && event.charCode <= 57'

将step属性设置为任何浮点数,例如0.01,你就可以走了。

您现在可以在Firefox input[inputmode]input的文本input限制为数字input[inputmode] ,请参阅Bruce Lawson关于input[inputmode]的新规范的文章 。

顺便说一下,如果你在“数字”input中有一个数字以外的任何东西,那么val()等返回“'。 我刚刚进行了检查,似乎为我所需要的(限制字母和其他字符)工作。

if( var == '' ) { alert( 'Var is not a number' ); }

与前导零,小数点等一起工作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值