我希望id=demo (/document.getElementById("demo").innerHTML)能像****那样输出值myInput3。
当前行为以纯文本显示myInput3的值。
也就是说,如果我在myInput3中输入********,我想在id=demo中看到********
<script>
function myFunction() {
  var x = document.getElementById("myInput").value + ' ' + document.getElementById("myInput2").value + ' ' + document.getElementById("myInput3").value;
  document.getElementById("myInput3").value;
  document.getElementById("demo").innerHTML = "Your details are: " + x;
}
</script>
<p>Write something in the text field to trigger a function.</p>
<div>Name:</div> <div><input type="text" id="myInput" oninput="myFunction()"></div>
<div>Username:</div> <div><input type="text" id="myInput2" oninput="myFunction()"></div>
<div>Password:</div> <div><input type="password" id="myInput3" oninput="myFunction()"><div>
<p id="demo"></p>
发布于 2020-02-12 02:58:58
可以使用*将字符( repeat() )重复到密码字段中字符的长度。
 var pass = document.getElementById("myInput3").value.trim();
 pass = '*'.repeat(pass.length);
<script>
function myFunction() {
  var pass = document.getElementById("myInput3").value.trim();
  pass = '*'.repeat(pass.length);
  var x = document.getElementById("myInput").value + ' ' + document.getElementById("myInput2").value + ' ' + pass;
  document.getElementById("myInput3").value;
  document.getElementById("demo").innerHTML = "Your details are: " + x;
}
</script>
<p>Write something in the text field to trigger a function.</p>
<div>Name:</div> <div><input type="text" id="myInput" oninput="myFunction()"></div>
<div>Username:</div> <div><input type="text" id="myInput2" oninput="myFunction()"></div>
<div>Password:</div> <div><input type="password" id="myInput3" oninput="myFunction()"><div>
<p id="demo"></p>
发布于 2020-02-12 03:04:15
可以使用regex替换密码中的所有字符,如下所示:
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction() {
  var x = document.getElementById("myInput").value + ' ' + 
  document.getElementById("myInput2").value + ' ' + 
  document.getElementById("myInput3").value.replace(/[\S]/g, "*")
  document.getElementById("demo").innerHTML = "Your details are: " + x;
}
</script>
<p>Write something in the text field to trigger a function.</p>
<div>Name:</div> <div><input type="text" id="myInput" oninput="myFunction()"></div>
<div>Username:</div> <div><input type="text" id="myInput2" oninput="myFunction()"></div>
<div>Password:</div> <div><input type="password" id="myInput3" oninput="myFunction()"><div>
<p id="demo"></p>
</body>
</html>
function myFunction() {
  var x = document.getElementById("myInput").value + ' ' + document.getElementById("myInput2").value + ' ' + document.getElementById("myInput3").value.replace(/[\S]/g, "*")
  document.getElementById("myInput3").value;
  document.getElementById("demo").innerHTML = "Your details are: " + x;
}<!DOCTYPE html>
<html>
<body>
<p>Write something in the text field to trigger a function.</p>
<div>Name:</div> <div><input type="text" id="myInput" oninput="myFunction()"></div>
<div>Username:</div> <div><input type="text" id="myInput2" oninput="myFunction()"></div>
<div>Password:</div> <div><input type="password" id="myInput3" oninput="myFunction()"><div>
<p id="demo"></p>
</body>
</html>
https://stackoverflow.com/questions/60180395
复制相似问题