[英]Replace comma with dot in a input Javascript
How to replace automatically all commas from a input with dots? 如何用点自动替换输入中的所有逗号?
I have this input: 我有这个输入:
<input type="text" id="test" onChange="testfunc()"> </input>
And this is my work so far: 到目前为止,这是我的工作:
function testfunc() {
var testnr = document.getElementById("test").value;
testnr = testnr.replace(/,/g, '.');
}
testnr
will store the value, so this testnr = testnr.replace(/,/g, '.');
testnr
将存储该值,因此此testnr = testnr.replace(/,/g, '.');
will only change the value without changing the ÌNPUT
, please try this: 只会更改值而不更改
ÌNPUT
,请尝试以下操作:
<input type="text" id="test" onchange="this.value = this.value.replace(/,/g, '.')"/>
Assign it to the DOM element, not the same variable 将其分配给DOM元素,而不是同一变量
function testfunc() {
var testnr = document.getElementById("test").value;
document.getElementById("test").value = testnr.replace(/,/g, '.');
}
替换后未将其分配回输入的值
you can do it in much simpler way if its just one input element 如果它只是一个输入元素,则可以用更简单的方式进行操作
<input type="text" onChange="this.value=this.value.replace(/,/g, '.');"/>
if there are multiple elements you want to apply this function to , then use in this style 如果要将此功能应用于多个元素,则以这种样式使用
<input type="text" onChange="myFunction(this)"/>
<script>
function myFunction(e) {
e.value=e.value.replace(/,/g, '.')
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.