【问题标题】:How to show and hide password when click on eye icon using jquery使用jquery单击眼睛图标时如何显示和隐藏密码
【发布时间】:2018-07-27 07:14:36
【问题描述】:

我需要在点击眼睛图标时显示和隐藏用户密码,所以我为此编写了脚本,当我点击眼睛图标时,只有类在改变,但密码不可见,再次点击斜线图标应该隐藏了这两种方法都不起作用如何解决这个问题?

<input type="password" name="player_password" id="pass_log_id" />

<span toggle="#password-field" class="fa fa-fw fa-eye field_icon toggle-password"></span>

<script>
$("body").on('click','.toggle-password',function(){
    $(this).toggleClass("fa-eye fa-eye-slash");

    var input = $("#pass_log_id").attr("type");

    if (input.attr("type") === "password") {
        input.attr("type", "text");
    } else {
        input.attr("type", "password");
    }
});
</script>

【问题讨论】:

标签: javascript jquery


【解决方案1】:

您的input 实际上是字符串。检查控制台,您应该看到该字符串没有方法attr(),因为您将$().attr() 分配给input

$("body").on('click', '.toggle-password', function() {
  $(this).toggleClass("fa-eye fa-eye-slash");
  var input = $("#pass_log_id");
  if (input.attr("type") === "password") {
    input.attr("type", "text");
  } else {
    input.attr("type", "password");
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span toggle="#password-field" class="fa fa-fw fa-eye field_icon toggle-password">Show/Hide</span>
<input type="password" id="pass_log_id"/>

【讨论】:

    【解决方案2】:

    您必须从您的 var input = $("#pass_log_id").attr("type"); 中删除 var .attr("type");
    您还可以使用ternary operator 更优雅地在type textpassword 之间切换:

    $(document).on('click', '.toggle-password', function() {
    
        $(this).toggleClass("fa-eye fa-eye-slash");
        
        var input = $("#pass_log_id");
        input.attr('type') === 'password' ? input.attr('type','text') : input.attr('type','password')
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" />
     
    <body>
    <input id="pass_log_id" type="password" name="pass" value="MySecretPass">
    <span toggle="#password-field" class="fa fa-fw fa-eye field_icon toggle-password"></span>
    
    </body>

    【讨论】:

    • 如果你要三元,那就一路走吧:input.attr('type', input.attr('type') === 'password' ? 'text' : 'password')
    【解决方案3】:

    <input type="checkbox" onclick="myFunction()">Show <input type="password" id="myInput" value="Password">
    
    
    <script>
      function myFunction() {
        var x = document.getElementById("myInput");
        if (x.type === "password") {
          x.type = "text";
        } else {
          x.type = "password";
        }
      }
    </script>

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    • @adiga 感谢您的宝贵时间
    【解决方案4】:

    请更换

    var input = $("#pass_log_id").attr("type");

    var 输入 = $("#pass_log_id");

    你需要的是元素而不是属性,

    【讨论】:

      【解决方案5】:

      HTML 代码

      <input type="password" placeholder="Password" id="pwd" class="masked" name="password" 
      />
      <button type="button" onclick="showHide()" id="eye">
         <img src="eye.png" alt="eye"/>
       </button>
      

      jquery 代码

      $(document).ready(function () {
          $("#eye").click(function () {
              if ($("#password").attr("type") === "password") {
                  $("#password").attr("type", "text");
              } else {
                  $("#password").attr("type", "password");
              }
          });
      });
      

      【讨论】:

        【解决方案6】:

        const togglePasswordEye = '<i class="fa fa-eye toggle-password-eye"></i>';
        const togglePasswordEyeSlash = '<i class="fa fa-eye-slash toggle-password-eye"></i>';
        
        $(togglePasswordEyeSlash).insertAfter('input[type=password]');
        $('input[type=password]').addClass('hidden-pass-input')
        
        $('body').on('click', '.toggle-password-eye', function (e) {
            let password = $(this).prev('.hidden-pass-input');
        
            if (password.attr('type') === 'password') {
                password.attr('type', 'text');
                $(this).addClass('fa-eye').removeClass('fa-eye-slash');
            } else {
                password.attr('type', 'password');
                $(this).addClass('fa-eye-slash').removeClass('fa-eye');
            }
        })
        .toggle-password-eye {
            float: right;
            top: -25px;
            right: 10px;
            position: relative;
            cursor: pointer;
        }
        <link href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" rel="stylesheet"/>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        
        <input id="password" type="password" class="form-control" name="password" required autocomplete="current-password">

        输出:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-18
          • 2019-10-26
          • 1970-01-01
          • 1970-01-01
          • 2017-10-25
          • 1970-01-01
          相关资源
          最近更新 更多