简体   繁体   English

jQuery输入数字带逗号

[英]Jquery input number with comma

I'm having a strange behavior in jquery. 我在jquery中有一个奇怪的行为。

I'd like to get the value of a input field (type number). 我想获取输入字段(类型编号)的值。 It's easier to show it with an example. 通过示例更容易展示它。

The function throws back an empty string when the value contains a comma. 当值包含逗号时,该函数将返回一个空字符串。

$("#input[type='number']").on('change',function() {
            alert($(this).val());
        });

Why? 为什么?

Thanks in advance 提前致谢

HTML5's spec doesn't allow commas: HTML5的规范不允许使用逗号:

value = floating-point number
   A string representing a number

floating-point numbe r: 浮点数

A floating-point number consists of the following parts, in exactly the following order:

    1. Optionally, the first character may be a "-" character.
    2. One or more characters in the range "0—9".
    3. Optionally, the following parts, in exactly the following order:
        1. a "." character
        2. one or more characters in the range "0—9"
    Optionally, the following parts, in exactly the following order:
        1. a "e" character or "E" character
        2. optionally, a "-" character or "+" character
        3. One or more characters in the range "0—9".

Add this to the input: step="0.01" 将此添加到输入中:step =“ 0.01”

so something like: 所以像这样:

<input type="number"  step="0.01" />

Then 0.10 will be valid, I am not sure if comma will be treated as valid if your operating system use decimals as comma separated. 那么0.10将是有效的,如果您的操作系统使用小数点作为逗号分隔,我不确定是否将逗号视为有效。

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

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