在文本框内开始键入文字时的jQuery文本

时间:2018-09-22 13:52:54

标签: javascript jquery html textbox

我想在开始在文本框中输入内容时将Forgot文本替换为View,以便用户可以查看他输入的密码,如果密码文本框为空,则默认文本显示为{{1 }}。如果显示Forgot并使用单击View链接,则在文本框中显示文本,或用View替换View中的文本,如果单击Hide,则文本框文本将成为密码格式。对jquery不熟悉,请提供任何帮助以帮助我实现它。

我的代码如下:

Hide

2 个答案:

答案 0 :(得分:2)

如果我没有误解您的要求,那么您可以尝试使用keyup事件

let link = $('#link');
let password = $('#password');
password.keyup(function() {
  if (!!this.value) {
    $('#link').text('View');
  } else {
    $('#link').text('Forgot?');
  }

  link.click(function(e) {
    e.preventDefault();
    if (this.text === "View") {
      $("#password").attr('type', 'text');
      $('#link').text('Hide');
    } else {
      $("#password").attr('type', 'password');
      $('#link').text('View');
    }
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input_field">
  <span><i class="fa fa-lock" aria-hidden="true"></i></span>
  <input type="password" class="password" id="password" name="password" placeholder="Password" maxlength="30" minlength="8" autocomplete="new-password" /><a class="hyperlink link" href="forgot.com" id="link" style="float: right; margin-right: 5px;position: absolute;top: 7px;right: 0px;"
    title="I forgotten my password">Forgot?</a></div>

答案 1 :(得分:2)

在下面更改您的html代码类似的代码。 (添加另一个anchor以切换密码可见性,并将data-l属性添加到anchors中以在js代码中使用):

window.onload=function(){
   var links=document.querySelectorAll("[data-l]");
   var pass=document.querySelector("#password");
   pass.addEventListener("keyup", function(){
        links.forEach(function(l){
            l.style.display= !!pass.value.trim().length==(l.getAttribute("data-l")=="true")?"":"none";
         })
   });
   document.querySelector("[data-l='true']").addEventListener("click", function(){
      pass.type=pass.type=="password"?"text":"password";
      this.innerHTML = pass.type=="text" ? "Hide Password" : "Show Password";
   });
};
<div class="input_field">
  <span>
         <i class="fa fa-lock" aria-hidden="true"></i>
  </span>
  <input type="password" class="password" id="password" name="password" placeholder="Password" maxlength="30" minlength="8" autocomplete="new-password"/>
   <a class="hyperlink link" data-l="false" href="forgot.com" id="link" style=" margin-right: 5px;position: absolute;top: 7px;right: 0px;" title="I forgotten my password" >Forgot?</a>
   <a class="hyperlink link" data-l="true" href="javascript:;" id="link" style="display:none;margin-right: 5px;position: absolute;top: 7px;right: 0px;" title="toggle password" >Show Password</a>
</div>


新部分: 如果要显示眼睛图标而不是anchor的文本,可以使用以下代码:

window.onload=function(){
    var links=document.querySelectorAll("[data-l]");
   var pass=document.querySelector("#password");
   pass.addEventListener("keyup", function(){
        links.forEach(function(l){
             l.style.display= !!pass.value.trim().length==(l.getAttribute("data-l")=="true")?"":"none";
         })
   });
   document.querySelector("[data-l='true']").addEventListener("click", function(){
       pass.type=pass.type=="password"?"text":"password";
       this.setAttribute("data-tp", pass.type=="text" ? "hide" : "show");
   });
};
[data-tp='hide']{
    display: inline-block;
    width: 25px;
    height: 25px;
    background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAPRSURBVGhD7ZpNqFVVGIZvZpaammkUERiikGZY/kwjoShKGzVI0YkOq5GgDQpDQQVTC5w0ybCQoJASKwKJKArNLHGiiElCNZAi0+zXyufJu+RztfY5+96uR/fhvPBwz1177bW/d++zvv2tdW9fTz11VtPguvMfm6tZ8AO8B40145PQxD/9NNaMQRt8MtJoM9fCu9AVZkbAO9A1ZnZB15jZCV1j5m3IzTiXGifNvAV1zIyEe+BxeAJW9uPnhTAbRsNlk2Z2QMnMXFgHe+BPiH1KnIX9sBHuhWHQUWnmTYhBnc5+HwzfwDNwI3RMJTM5v8Nh+BjegNfhffgSfoXSOfIzvACToCO6BnIzf4Ffr/ugVVbz3JmwHD4Ev2ZxHPFGONYoGFI9Crvhpn9/O6+SmcGk5ttgNZyAOJZ8DV77f2ssbIU08AGYCEntzPhzTj/tDC6AOE7kNRgDg5KZyDuSD/oFxElZZeZJ+DG0mRRWwdVQ0lVwCOI4kSNwNwxI5vtfIA60D9KFPocbIKlkpoqXoUoaTf28lteM5/4Gi6CWnoO/IZ3sZ/O974vv+tvkEYgaiBnfHSXdD6mP1/KamyCP51loqQ2QThAn4MOgfPTx2F2Qq66ZF6GkyRD7eU3lTfse4rHNkI5fkA0eiB2PwhRIcuLH4zdDSXXM+F4pyUQS+8UJPhWMKR73nXORGXN27PAZlAKNOf9OGyrUzsx6KMmldurzhw2ZjMnY4ljGfkG5ESfzLZDrW0h9HrKhharMOGG9uyU5Zupn2ZLLmIwtjneRER+P39vY4Tjkdz2uTZ63oY1KZg5C1RLAMVM/C9QoY8lfB1ugOE/MELHjSXgQkiwr0rGvoE7VWjJTWgL4fnHM1OcpSHoA4jtJNP0fE1FPQ0x31lBrYTjcCnGeLIY6qmNmCaRjzg/LIa+5BowhHTM2b2gtuSg6A+lk2QumXDNOavNRj4M6amVmPMSvzTaYDp+ENvkJHoMBaQbkJYN3yrI8trlBUVV25KoyEzc5vOOvghVw7OcSoCpJtNX1EIvGKrZD3aq3ZKYdL8FAq+qiLB3yl1GOXz2fYh3VNXMMYrIZErmp4LrhFJQuKk7KV2AeOFFbqbQHkEiV8iXdpJgAZhInXimIhOnyUzBYE4SrwQ8gVs2ayTcBTfm3Q8dkplkBrdYQJSzNczOlveaql+YllftYltYfgRsIMagSPqkYqJ8NPva5bGaSTMV3gHXTUkgbdMtgPpgQnOy5qswMSbbqtLrKjEH3zFyJ6pm5UtXVZvwTuuv7RiqZ0YT/1NBoaaaxT6KnBqqv7xy8PjUHNT8LeQAAAABJRU5ErkJggg==);
    background-size: 100%;
}
[data-tp='show']{
    display: inline-block;
    width: 25px;
    height: 25px;
    background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAANqSUR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x1oF+xyw+CDzqeurwi9ot2QS2/wMj2lahgOeoXgGbhDLkH6g87Nl/jhhm5qampqampqamvsiPT3/AVfe3H38IFoKAAAAAElFTkSuQmCC);
    background-size: 100%;
}
<div class="input_field">
  <span>
         <i class="fa fa-lock" aria-hidden="true"></i>
  </span>
  <input type="password" class="password" id="password" name="password" placeholder="Password" maxlength="30" minlength="8" autocomplete="new-password"/>
   <a class="hyperlink link" data-l="false" href="forgot.com" id="link" style=" margin-right: 5px;position: absolute;top: 7px;right: 0px;" title="I forgotten my password" >Forgot?</a>
   <a class="hyperlink link" data-l="true" href="javascript:;" id="link" style="display:none;margin-right: 5px;position: absolute;top: 7px;right: 0px;" title="toggle password" data-tp='show'></a>
</div>