[英]How can one make <input> to show *** instead as plain text
我希望id=demo (/document.getElementById("demo").innerHTML)
的值myInput3像****
一樣輸出。
當前行為以純文本形式顯示myInput3的值。
即如果我在myInput3 中輸入abcd1234
,我想在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>
您可以使用repeat()
將字符 ( *
) 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>
您可以使用正則表達式替換密碼中的所有字符,如下所示:
<!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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.