2014-09-30 53 views
1

我在jquery中有一个奇怪的行为。用逗号输入数字jquery

我想获得输入字段(类型号)的值。以示例显示它更容易。

当该值包含逗号时,函数将返回空字符串。

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

为什么?

在此先感谢

+3

因为例如'1,2',不是一个数字。 – 2014-09-30 14:53:02

+0

我试过你的代码,它工作,我只删除_#_。你能分享你的HTML吗? – 2014-09-30 14:55:55

+0

@Ben我知道1,2号不是一个数字,但我需要对待这个用点替换逗号。或者你知道任何其他方法来做到这一点? – Manuel 2014-09-30 15:01:39

回答

3

HTML5的spec不允许逗号:

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". 
+0

这是否考虑到本地化?一些国家使用逗号作为小数点分隔符。 – 2014-09-30 14:58:45

+1

正如所写,规范中没有任何内容允许它。浏览器可能选择将本地格式转换为HTML5允许的格式,但这是未定义的行为。 – 2014-09-30 15:00:02

+0

那么,我的问题是用点替换逗号,因为我需要将我的数据添加到sql数据库中,有什么办法吗? – Manuel 2014-09-30 15:08:32

-1

一下添加到输入:步= “0.01”

所以像这样:

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

然后0.10将是有效的,我不确定如果您的操作系统使用逗号分隔逗号作为逗号将被视为有效。

相关问题