![](/img/trans.png)
[英]for input type="number" how to set default value to 0
[英]Set value to Input type number not working correctly
我想使用Jquery设置一个值来输入类型number
。 当值的长度超过15个字符时,它将不起作用。
这是我的示例代码:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#user").val(11111111111111444444444888888888);
});
});
</script>
</head>
<body>
<p>Name: <input type="number" id="user" ></p>
<button>Set the value of the input field</button>
</body>
</html>
结果是:
11111111111111400000000000000000
这对我来说是意外的。 为什么它在15个字符后变为零? 当数字的长度小于15时,它工作正常。我尝试使用min
和max
属性,但这也不起作用。 任何想法?
我认为这是因为javascript数字始终是64位浮点。
与许多其他编程语言不同,JavaScript不会定义不同类型的数字,例如整数,短整数,长整数,浮点数等。
遵循国际IEEE 754标准,JavaScript数字始终存储为双精度浮点数。
例子:
var x = 999999999999999; // x will be 999999999999999
var y = 9999999999999999; // y will be 10000000000000000
从W3Schools收集的示例和示例。
也许您可以在val()
中添加''
以将值作为字符串传递。
正如Goediaz对这种行为的解释一样,
您可能希望将其作为字符串放在val()
中。
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("#user").val(11111111111111444444444888888888); console.log("Number:", $("#user").val()); $("#user").val("11111111111111444444444888888888"); console.log("String:", $("#user").val()); }); }); </script> </head> <body> <p>Name: <input type="number" id="user" ></p> <button>Set the value of the input field</button> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.