简体   繁体   English

在Android设备和浏览器中捕获输入类型编号的点

[英]Capture dot for Input type number in Android Device and Browser

In below snippet code, If i give inputs "9999999999" and "9999999999." 在下面的代码段中,如果我输入“ 9999999999”和“ 9999999999”。 in console log both displays the same result "999999999" with out dot. 在控制台日志中,两者都显示相同的结果“ 999999999”,但不带点。

Am trying to capture dot symbol even after number. 我试图甚至在数字后捕获点符号。 Here, only input type number is allowed cannot use text or any other type. 在此,仅允许输入类型的数字不能使用文本或任何其他类型。

Text box is for number 文字框代表数字

Number: <input type='number' id=txtNumber'/>

And it's javascript function 这是javascript函数

$(document).ready(function() {  
   $('#txtNumber').keyup(function () {
     var numbers = $(this).val(); console.log("numbers", numbers);
   });
});

code at JSFiddle JSFiddle上的代码

Please help to find solution. 请帮助找到解决方案。

The reason is you set type='number' . 原因是您设置type='number' I suggest you set type='text' and use regular expressions to check if the value in the input is a number(include dot) or not: 我建议您设置type='text'并使用正则表达式检查输入中的值是否为数字(包括点):

  $(document).ready(function() {  
       $('#txtNumber').keyup(function () {
        if ($(this).val().match(/^[0-9]*\.?[0-9]*$/)){ //check if your value is number or not
         var numbers = $(this).val(); console.log("numbers", numbers);
         }
       });
    });

You can use a function to do this: 您可以使用一个函数来做到这一点:

$(document).ready(function () {
        $('#txtNumber').keyup(function () {
            var numbers = $(this).val(); console.log("numbers", numbers);
            // dump(numbers);
            numbers = parseInt(numbers).toFixed(2);
            console.log(numbers);
        });
    });

It will give you your desired output. 它将为您提供所需的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM