如何在输入类型数字中阻止+, - ,e? [英] How to block +,-,e in input type Number?

查看:190
本文介绍了如何在输入类型数字中阻止+, - ,e?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我输入字母数字时,字母 e 和特殊字符也显示在输入字段中。我只想显示数字。如何阻止它们?



< input type =number>


解决方案

默认行为,如果你不喜欢传入的键值:



document。 querySelector(。your_class)。addEventListener(keypress,function(evt){if(evt.which!= 8& evt.which!= 0& evt.which< 48 || evt ); // 0代表空值// 8代表退格空间// 48-57代表0-9数字

< input type =num berclass =your_class>


When I'm giving input type number the letter e and special charecters are also displaying in input field. I want to display only digits. How to block them?

<input type="number">

解决方案

Try preventing the default behaviour if you don't like the incoming key value:

document.querySelector(".your_class").addEventListener("keypress", function (evt) {
    if (evt.which != 8 && evt.which != 0 && evt.which < 48 || evt.which > 57)
    {
        evt.preventDefault();
    }
});

// 0 for null values
// 8 for backspace 
// 48-57 for 0-9 numbers

<input type="number" class="your_class">

这篇关于如何在输入类型数字中阻止+, - ,e?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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