首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使<input>以纯文本形式显示*

如何使<input>以纯文本形式显示*
EN

Stack Overflow用户
提问于 2020-02-12 02:52:39
回答 2查看 51关注 0票数 1

我希望id=demo (/document.getElementById("demo").innerHTML)能像****那样输出值myInput3。

当前行为以纯文本显示myInput3的值。

也就是说,如果我在myInput3中输入********,我想在id=demo中看到********

代码语言:javascript
运行
复制
<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>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-12 02:58:58

可以使用*将字符( repeat() )重复到密码字段中字符的长度。

代码语言:javascript
运行
复制
 var pass = document.getElementById("myInput3").value.trim();
 pass = '*'.repeat(pass.length);

代码语言:javascript
运行
复制
<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>

票数 0
EN

Stack Overflow用户

发布于 2020-02-12 03:04:15

可以使用regex替换密码中的所有字符,如下所示:

代码语言:javascript
运行
复制
<!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>

代码语言:javascript
运行
复制
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;
}
代码语言:javascript
运行
复制
<!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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60180395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档