javascript 如何使用jquery在点击眼睛图标时显示和隐藏密码

k97glaaz  于 2023-01-16  发布在  Java
关注(0)|答案(7)|浏览(238)

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

<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>
qnyhuwrf

qnyhuwrf1#

您的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"/>
qxgroojn

qxgroojn2#

您必须从var input = $("#pass_log_id").attr("type");中删除变量.attr("type");
您还可以使用ternary operatortype 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>
dwbf0jvd

dwbf0jvd3#

<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>
omjgkv6w

omjgkv6w4#

请更换
变量输入= $(“#传递日志ID”).属性(“类型”);

变量输入= $(“#通过日志ID”);
你需要的是元素而不是属性

dsekswqp

dsekswqp5#

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");
        }
    });
});
brjng4g3

brjng4g36#

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">

输出:

bxjv4tth

bxjv4tth7#

这是一个简单的脚本,带有密码切换处理Html和javascript代码

<div class="input-box">
    <input type="password" placeholder="Password" id="pass">
    <img src="eye-close.png" onclick="handle()" id="eyeicon">
</div>

<script>

    function handle() {

        var pass = document.getElementById('pass')
        var eyeicon = document.getElementById('eyeicon')

        if (pass.type == "password") {
            pass.type = "text"
            eyeicon.src = "eye-open.png";
        } else {
            pass.type = "password"
            eyeicon.src = "eye-close.png";
        }
    }

</script>

相关问题