【问题标题】:How to add an icon in password field to show & hide password in rails如何在密码字段中添加图标以在 Rails 中显示和隐藏密码
【发布时间】:2021-10-21 06:42:27
【问题描述】:

我的代码:

div.form-group.col-md-12
              label =t('.password')
              = f.text_field :password,
                      required: true,
                      label: false,
                      type: 'password',
                      placeholder: 'Password',
                      input_html: {autocomplete: "new-password"},
                      class: 'form-control input-sm form-control-sm'

此表单域用于输入密码。我想在输入框右侧有一个眼睛按钮,它可以切换密码显示和隐藏。我知道它可以由 JS 完成。但我不知道该怎么做。

【问题讨论】:

    标签: javascript ruby-on-rails


    【解决方案1】:

     $("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"/>

    => 代表html你可以使用这段代码

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

    【讨论】:

    • 这可行,但它带有一个新框并且没有出现眼睛按钮
    • 为此您需要应用图标代表显示/隐藏
    • 我是新手。你能通过提供代码来帮助解决这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 2022-10-24
    • 2011-06-10
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多