简体   繁体   English

密码显示/隐藏 fontawesome 图标问题,未放入输入字段

[英]password show/hide fontawesome icon problem and not placed into input field

Here is my code:这是我的代码:

  <div class="form-group">
<label>Password</label>
<div class="input-group" id="show_hide_password">
 <input class="form-control" name="password" type="password">
 <div class="input-group-addon">
 <a href=""><i class="fas fa-eye-slash" aria-hidden="true"></i> </a>
 </div>
</div>
</div>

its output is like this: Here is my code output screenshot它的 output 是这样的:这是我的代码 output screenshot

The problem is right side icon is:问题是右侧图标是:

  1. fa-eye-slash not changing to fa-eye fa-eye-slash 不更改为 fa-eye
  2. right side icon is not placed inside the input field右侧图标未放置在输入字段内

Update更新
I forget to post my javascript:我忘记发布我的 javascript:

$(document).ready(function() {
    $("#show_hide_password a").on('click', function(event) {
        event.preventDefault();
        if($('#show_hide_password input').attr("type") == "text"){
            $('#show_hide_password input').attr('type', 'password');
            $('#show_hide_password i').addClass( "fa-eye-slash" );
            $('#show_hide_password i').removeClass( "fa-eye" );
        }else if($('#show_hide_password input').attr("type") == "password"){
            $('#show_hide_password input').attr('type', 'text');
            $('#show_hide_password i').removeClass( "fa-eye-slash" );
            $('#show_hide_password i').addClass( "fa-eye" );
        }
    });
});

Thanks & Best Regards谢谢和最好的问候

As said in the comment, you need class="input-group-text" , also in 5.6 its input-group-append not input-group-addon .正如评论中所说,您需要class="input-group-text" ,在 5.6 中也是它的input-group-append而不是input-group-addon

Then to toggle the show/hide, put a class on the link, then on click, find the outer .input-group cache it into a var so can reuse it to find() the other elements, then toggle them, can use a ternary for input type and toggleClass() for the icon.然后切换显示/隐藏,将 class 放在链接上,然后单击,找到外部.input-group将其缓存到 var 中,以便可以重用它来查找()其他元素,然后切换它们,可以使用输入类型为三元,图标为toggleClass()

 $(document).ready(function() { // target the link $(".toggle_hide_password").on('click', function(e) { e.preventDefault() // get input group of clicked link var input_group = $(this).closest('.input-group') // find the input, within the input group var input = input_group.find('input.form-control') // find the icon, within the input group var icon = input_group.find('i') // toggle field type input.attr('type', input.attr("type") === "text"? 'password': 'text') // toggle icon class icon.toggleClass('fa-eye-slash fa-eye') }) })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css"> <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css" rel="stylesheet"> <div class="form-group"> <label>Password</label> <div class="input-group"> <input class="form-control" name="password" type="password"> <div class="input-group-append"> <span class="input-group-text"> <a href="#" class="toggle_hide_password"> <i class="fas fa-eye-slash" aria-hidden="true"></i> </a> </span> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM