[英]How to remove dot using replace javascript in input type number?
How to remove dot using replace javascript in input type number ? 如何使用输入类型编号中的替换javascript删除点?
When i tried fill 999.
into input. 当我尝试填写
999.
输入。 Why it's not remove dot .
为什么不删除点
.
How can i do ? 我能怎么做 ?
<input name="test" id="iid" onKeyUp="test_fn(this.value)" type="number"> <script> function test_fn(test_value){ var test_value = test_value.replace(/[^0-9]+/g, ""); document.getElementById("iid").value = test_value; } </script>
Perhaps you want to just not allow dots to be entered in the input field. 也许你想不允许在输入字段中输入点。 Here's one way of doing that:
这是一种方法:
document.getElementById('iid').addEventListener('keypress', test_fn); function test_fn(e){ if (e.charCode == 46) e.preventDefault(); // not allowed to type . }
<input name="test" id="iid" type="number">
I don't know the reason of such a behavior. 我不知道这种行为的原因。 What I've done to solve such a problem is that, I will clear the contents of the
input
each time a key is pressed. 我为解决这个问题所做的是,每次按下一个键,我都会清除
input
内容。
I've added the below given line to your Script which will reset the input
each time a key is pressed. 我已经在脚本中添加了以下给定的行,每次按下一个键时都会重置
input
。
document.getElementById("iid").value = "";
Please see the Snippet below 请参阅下面的代码段
function test_fn(test_value){ var test_value = test_value.replace(/[^0-9]+/g, ""); document.getElementById("iid").value = ""; document.getElementById("iid").value = test_value; }
<input name="test" id="iid" onKeyUp="test_fn(this.value)" type="number">
UPDATE UPDATE
As @AssafiCohen-Arazi mentioned in his comment, this answer will prove to be incorrect if someone keeps on pressing the .
正如@ AssafiCohen-Arazi在评论中提到的那样,如果有人继续按下,那么这个答案就会被证明是错误的
.
key for so long. 关键这么久。 Thus, the better solution would be the one that is mentioned in @James answer above.
因此,更好的解决方案将是上面@James回答中提到的解决方案。
UPDATE 2 更新2
Found out the reason why you were getting in-stable results. 找出你获得稳定结果的原因。 It was all because your
input
type was number. 这完全是因为您的
input
类型是数字。 You can just change your input
type to text
and your code will run perfectly. 您只需将
input
类型更改为text
,您的代码就可以完美运行。
See the snippet below 请参阅下面的代码段
function test_fn(test_value){ var test_value = test_value.replace(/[^0-9]/g, ""); document.getElementById("iid").value = test_value; }
<input name="test" id="iid" onKeyUp="test_fn(this.value)" type="text">
It is working fine for me, try this: 它对我来说很好,试试这个:
function test_fn(e){ var charCode = e.which || e.keyCode; if (charCode >= 48 && charCode <= 57 || charCode >= 96 && charCode <= 105) return true; e.preventDefault(); }
<input name="test" id="iid" onKeyDown="test_fn(event)" type="number">
<input name="test" id="iid" onKeyUp="test_fn(this.value)" type="text"> <script> function test_fn(test_value){ var test_value = test_value.replace(/[^0-9]+/g, ""); document.getElementById("iid").value = test_value; } </script>
The type=number
in the input field triggers a value sanitization algorithm hence you cannot get the actual value from the input field unless it's a valid floating point number. 输入字段中的
type=number
触发值清理算法,因此您无法从输入字段获取实际值,除非它是有效的浮点数。
You can work around this by using a text
type (since you're already checking numeric values with the regulare expression /[^0-9]+/g
您可以使用
text
类型解决此问题(因为您已经使用regularre表达式检查数值/[^0-9]+/g
<input name="test" id="iid" onKeyUp="test_fn(this.value)" type="text"> <script> function test_fn(test_value) { var test_value = test_value.replace(/[^0-9]+/g, ""); document.getElementById("iid").value = test_value; } </script>
See also: How to get the raw value an field? 另请参见: 如何将原始值作为字段?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.