javascript - 制作带点的输入字段掩码

标签 javascript jquery html

我有一个这样的输入框:

<input type="text" placeholder=".........">

当用户输入带有一些文本的字段时,我希望将这些点替换为字母。因此,如果我在其中输入“Ha”。用户看到这个:

<input type="text" placeholder="Ha.......">

其他字母依此类推

我看过很多带有日期、数字和 ip 的示例,但我仍然不确定如何将其重现到我的问题中。

最佳答案

这个问题的解决方案会稍微复杂一些。

首先,创建一个新的复制输入作为“占位符”,因为它将 float 在真实输入字段后面。然后在真实输入上附加事件监听器并将值填充到“占位符”。

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  </head>
  <body>
    <div style="position: relative; width: 400px; height: 24px;">
        <input type="text" style="width: 100%; height: 100%; position: absolute; z-index: 1; color: #ccc" id="placeholder">
        <input type="text" style="width: 100%; height: 100%; position: absolute; z-index: 2; background-color: transparent; color: #000" id="realInput">
    </div>
    <script>
      function fillPlaceholder(){
        //suppose you want 9 characters for the placeholder
        var limit = 9;
        var text = $("#realInput").val();
        while (text.length < limit) {
            text += ".";
        }
        $("#placeholder").val(text);
      }
      $("#realInput").on("input", fillPlaceholder);
      fillPlaceholder();
    </script>
  </body>
</html>

关于javascript - 制作带点的输入字段掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34793601/

相关文章:

php - 将 JavaScript 代码转换为 PHP : Determining what return (1) means in javascript

jquery - 移动 float div 时为其设置动画

jquery - iframe div 未关闭子按钮单击事件

javascript - 定位当前选中的单选按钮

python - 将提取代码添加到 beautifulsoup 循环

javascript - 在每个 src 或 href 自动值 "$1"$0

javascript - 使用 setAttribute 添加自定义外观控制

javascript - 重新订阅时,Pubnub 会收到所有消息

c# - 在 asp.net C# 页面中提醒

javascript - 如何在 jQuery 中设置下拉列表的 SELECTED 属性?