我有这个简单的输入:
<input type="number" step="0.01">
我怀疑/希望所有这些输入值成功地拥有这些输出:
2.00 => 2.00
2,00 => 2.00
+2,00 => 2.00
但“+2.00”的值失败。
+2.00 => ""
我想知道为什么会这样。 Javascript本身将“+2.00”识别为数字。
答案 0 :(得分:2)
应该没有问题,而不是您提供的代码。我创建了下面的示例,您可以在其中输入“+2.00”并点击按钮查看其结果。
我的发现:
✓Chrome60:
{
"nr": "2.00",
"type": "string",
"directAdd": "2.001",
"parseThenAdd": 3
}
✗Firefox54:
{
"nr": "",
"type": "string",
"directAdd": "1",
"parseThenAdd": NaN
}
✗Safari9.1:
{
"nr": "",
"type": "string",
"directAdd": "1",
"parseThenAdd": NaN
}
✓IEEdge 40 / EdgeHTML 15:
{
"nr": "+2.00",
"type": "string",
"directAdd": "+2.001",
"parseThenAdd": 3
}
✓IEIE 11:
{
"nr": "+2.00",
"type": "string",
"directAdd": "+2.001",
"parseThenAdd": 3
}
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById("btn").addEventListener("click", function(evt) {
var nr = document.getElementById("nr").value;
console.log({
nr: nr,
type: typeof nr,
directAdd: nr + 1.00,
parseThenAdd: parseInt(nr, 10) + 1.00
});
});
});
<p>Try inputting "+2.00", just works.</p>
<input id="nr" step="0.01" type="number">
<button id="btn">ConsoleLogIt!</button>
据我所知,应工作,因为按the spec链接到the rules for parsing the value包括:
- 醇>
如果位置指示的字符是U + 002D HYPHEN-MINUS字符( - ):
- 将值和除数更改为-1。
- 提升位置到下一个角色。
- 如果位置超过输入结束,则返回错误。
醇>否则,如果位置(第一个字符)指示的字符是“+”(U + 002B)字符:
- 提升位置到下一个角色。 (“+”被忽略,但不符合。)
- 如果位置超过输入结束,则返回错误。
据我所知,Firefox错误地不解析“+2.00”值。鉴于I could not find a bug yet我已经开始posted it to Bugzilla所以我们可以看到真正的专家对此有何看法: - )