简体   繁体   English

悬停图标时如何使密码文本框值可见

[英]how to make password textbox value visible when hover an icon

Good day all,大家好,

I have a form that has a password field:我有一个包含密码字段的表单:

<input type="password" name="password" size="30" />

Naturally, the input text will be replaced by (*).自然地,输入文本将被 (*) 替换。

So if the user typed 123 the box will show *** .因此,如果用户输入123该框将显示***

Up to here, it is straight forward, but...到这里,它是直截了当的,但是......

Now, I wanna add a small icon next to the password box so when the user hover over this icon, he can see what he has entered so far.现在,我想在密码框旁边添加一个小图标,这样当用户将鼠标悬停在这个图标上时,他就可以看到他到目前为止输入的内容。

So, while hovering, the box will show 123 and when the user leaves the icon the box should show *** again.因此,当悬停时,该框将显示123 ,当用户离开该图标时,该框应再次显示***

Is there any way to do this with JavaScript?有没有办法用 JavaScript 做到这一点? Also, I am using HTML and PHP.另外,我正在使用 HTML 和 PHP。

EDIT:编辑:

It really doesn't need to be an icon, it could be a checkbox or a button... AND if it could be done in CSS, I would really appreciate to know how它真的不需要是一个图标,它可以是一个复选框或一个按钮......如果它可以在 CSS 中完成,我真的很感激知道如何

PS I've googled and search the stackoverflow but with no luck PS我用谷歌搜索并搜索了stackoverflow但没有运气

You will need to get the textbox via javascript when moving the mouse over it and change its type to text .将鼠标移到文本框上并将其type更改为text时,您需要通过 javascript 获取文本框。 And when moving it out, you will want to change it back to password .移出时,您需要将其改回password No chance of doing this in pure CSS.没有机会在纯 CSS 中做到这一点。

HTML: HTML:

<input type="password" name="password" id="myPassword" size="30" />
<img src="theicon" onmouseover="mouseoverPass();" onmouseout="mouseoutPass();" />

JS: JS:

function mouseoverPass(obj) {
  var obj = document.getElementById('myPassword');
  obj.type = "text";
}
function mouseoutPass(obj) {
  var obj = document.getElementById('myPassword');
  obj.type = "password";
}

As these guys said, just change input type.正如这些人所说,只需更改输入类型。
But do not forget to change type back as well.但也不要忘记改回类型。

See my simple jquery demo: http://jsfiddle.net/kPJbU/1/请参阅我的简单 jquery 演示: http : //jsfiddle.net/kPJbU/1/

HTML: HTML:

<input name="password" class="password" type="password" />
<div class="icon">icon</div>

jQuery: jQuery:

$('.icon').hover(function () {
    $('.password').attr('type', 'text');
}, function () {
    $('.password').attr('type', 'password');
});

I use this one line of code, it should do it:我使用这一行代码,它应该这样做:

<input type="password"
       onmousedown="this.type='text'"
       onmouseup="this.type='password'"
       onmousemove="this.type='password'">

Complete example below.下面的完整示例。 I just love the copy/paste :)我只是喜欢复制/粘贴:)

HTML HTML

<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
           <div class="panel panel-default">
              <div class="panel-body">
                  <form class="form-horizontal" method="" action="">

                     <div class="form-group">
                        <label class="col-md-4 control-label">Email</label>
                           <div class="col-md-6">
                               <input type="email" class="form-control" name="email" value="">
                           </div>
                     </div>
                     <div class="form-group">
                       <label class="col-md-4 control-label">Password</label>
                          <div class="col-md-6">
                              <input id="password-field" type="password" class="form-control" name="password" value="secret">
                              <span toggle="#password-field" class="fa fa-lg fa-eye field-icon toggle-password"></span>
                          </div>
                     </div>
                 </form>
              </div>
           </div>
      </div>
  </div>

CSS CSS

.field-icon {
  float: right;
  margin-right: 8px;
  margin-top: -23px;
  position: relative;
  z-index: 2;
  cursor:pointer;
}

.container{
  padding-top:50px;
  margin: auto;
}

JS JS

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

Try it here: https://codepen.io/anon/pen/ZoMQZP在这里试试: https : //codepen.io/anon/pen/ZoMQZP

In one line of code as below :在如下一行代码中:

 <p> cursor on text field shows text .if not password will be shown</p> <input type="password" name="txt_password" onmouseover="this.type='text'" onmouseout="this.type='password'" placeholder="password" />

1 minute googling gave me this result . 1 分钟的谷歌搜索给了我这个结果 See the DEMO !演示

HTML HTML

<form>
    <label for="username">Username:</label>
    <input id="username" name="username" type="text" placeholder="Username" />
    <label for="password">Password:</label>
    <input id="password" name="password" type="password" placeholder="Password" />
    <input id="submit" name="submit" type="submit" value="Login" />
</form>

jQuery jQuery

// ----- Setup: Add dummy text field for password and add toggle link to form; "offPage" class moves element off-screen
$('input[type=password]').each(function () {
    var el = $(this),
        elPH = el.attr("placeholder");
    el.addClass("offPage").after('<input class="passText" placeholder="' + elPH + '" type="text" />');
});
$('form').append('<small><a class="togglePassText" href="#">Toggle Password Visibility</a></small>');

// ----- keep password field and dummy text field in sync
$('input[type=password]').keyup(function () {
    var elText = $(this).val();
    $('.passText').val(elText);
});
$('.passText').keyup(function () {
    var elText = $(this).val();
    $('input[type=password]').val(elText);
});

// ----- Toggle link functionality - turn on/off "offPage" class on fields
$('a.togglePassText').click(function (e) {
    $('input[type=password], .passText').toggleClass("offPage");
    e.preventDefault(); // <-- prevent any default actions
});

CSS CSS

.offPage {
    position: absolute;
    bottom: 100%;
    right: 100%;
}

Try This :尝试这个 :

In HTML and JS :在 HTML 和 JS 中:

 // Convert Password Field To Text On Hover. var passField = $('input[type=password]'); $('.show-pass').hover(function() { passField.attr('type', 'text'); }, function() { passField.attr('type', 'password'); })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <!-- An Input PassWord Field With Eye Font-Awesome Class --> <input type="password" placeholder="Type Password"> <i class="show-pass fa fa-eye fa-lg"></i>

Its simple javascript.它的简单 javascript。 Done using toggling the type attribute of the input.使用切换输入的type属性完成。 Check this http://jsfiddle.net/RZm5y/16/检查这个http://jsfiddle.net/RZm5y/16/

   <script>
       function seetext(x){
           x.type = "text";
       }
       function seeasterisk(x){
          x.type = "password";
       }
   </script>
  <body>
    <img onmouseover="seetext(a)" onmouseout="seeasterisk(a)" border="0" src="smiley.gif"   alt="Smiley" width="32" height="32">
   <input id = "a" type = "password"/>
 </body>

Try this see if it works试试这个看看它是否有效

a rapid response not tested on several brosers, works on gg chrome / win快速响应未在几个兄弟上测试,适用于 gg chrome / win

-> On focus event -> show/hide password -> 焦点事件 -> 显示/隐藏密码

<input type="password" name="password">

script jQuery脚本jQuery

// show on focus
$('input[type="password"]').on('focusin', function(){

    $(this).attr('type', 'text');

});
// hide on focus Out
$('input[type="password"]').on('focusout', function(){

    $(this).attr('type', 'password');

});
<html>
<head>
</head>
<body>
<script>
function demo(){
    var d=document.getElementById('s1');
    var e=document.getElementById('show_f').value;
    var f=document.getElementById('show_f').type; 

    if(d.value=="show"){
        var f= document.getElementById('show_f').type="text";
        var g=document.getElementById('show_f').value=e;
        d.value="Hide";

    } else{
        var f= document.getElementById('show_f').type="password";
        var g=document.getElementById('show_f').value=e;
        d.value="show";
     }     
  }   
</script>   

<form method='post'>
Password: <input type='password' name='pass_f' maxlength='30' id='show_f'><input type="button" onclick="demo()" id="s1" value="show" style="height:25px; margin-left:5px;margin-top:3px;"><br><br>
<input type='submit' name='sub' value='Submit Now'>

</form>
</body>
</html>

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

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