[英]how to return new input value
i would like to replace my old input value (ancien_tel) by (nouveau_tel) to reformat my phone number on blur i missing something to return the value into my input text box.我想用 (nouveau_tel) 替换我的旧输入值 (ancien_tel) 以在模糊时重新格式化我的电话号码我缺少一些东西以将值返回到我的输入文本框中。
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<meta charset="UTF-8">
<script>
function reformatter() {
var ancien_tel = document.querySelector("#telephone").value;
var nouveau_tel = "(" + ancien_tel.substring(0, 3) + ") " + ancien_tel.substring(3, 6) + "-" + ancien_tel.substring(6).value;
ancien_tel = nouveau_tel;
}
</script>
</head>
<body>
Telephone:<br>
<input type="text" id="telephone" onblur="reformatter()" maxlength="10">
</body>
</html>
Instead of doing而不是做
ancien_tel = nouveau_tel
, do: , 做:
document.querySelector('#telephone').value = nouveau_tel
ancien_tel
's value is copied from the #telephone
element's value, it does not refer to what's inside the #telephone
element. ancien_tel
的值是从#telephone
元素的值复制而来的,它不引用#telephone
元素中的内容。 A bit less verbose code could be something like:稍微不那么冗长的代码可能是这样的:
function reformatter() {
const telephone = document.querySelector("#telephone");
const ancien_tel = telephone.value;
const nouveau_tel = "(" + ancien_tel.substring(0, 3) + ") " + ancien_tel.substring(3, 6) + "-" + ancien_tel.substring(6).value;
telephone.value = nouveau_tel;
}
All you need to change the reformatter function更改重新格式化程序功能所需的一切
function reformatter() {
var ancien_tel = document.querySelector("#telephone").value;
var nouveau_tel =
"(" +
ancien_tel.substring(0, 3) +
") " +
ancien_tel.substring(3, 6) +
"-" +
ancien_tel.substring(6);
document.querySelector("#telephone").value = nouveau_tel;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.