Jquery show/hide password

蹲街弑〆低调 提交于 2019-12-05 04:17:10

问题


Below Jquery is working fine but How could I shorten this Jquery? I have to show and hide the password for password and confirmed password? https://jsfiddle.net/c5cvLo54/1/

$(".password-showhide .show-password").click(function() {
    $("#password").attr("type", "text");
    $(".password-showhide .show-password").hide();
    $(".password-showhide .hide-password").show();
});
$(".password-showhide .hide-password").click(function() {
    $("#password").attr("type", "password");
    $(".password-showhide .hide-password").hide();
    $(".password-showhide .show-password").show();
});

$(".confirm-password-showhide .show-password").click(function() {
    $("#confirmPassword").attr("type", "text");
    $(".confirm-password-showhide .show-password").hide();
    $(".confirm-password-showhide .hide-password").show();
});
$(".confirm-password-showhide .hide-password").click(function() {
    $("#confirmPassword").attr("type", "password");
    $(".confirm-password-showhide .hide-password").hide();
    $(".confirm-password-showhide .show-password").show();
});

回答1:


Here is one-of-the ways, how you can shorten your code:

$(document).ready(function() {

  $(".show-password, .hide-password").on('click', function() {
    var passwordId = $(this).parents('li:first').find('input').attr('id');
    if ($(this).hasClass('show-password')) {
      $("#" + passwordId).attr("type", "text");
      $(this).parent().find(".show-password").hide();
      $(this).parent().find(".hide-password").show();
    } else {
      $("#" + passwordId).attr("type", "password");
      $(this).parent().find(".hide-password").hide();
      $(this).parent().find(".show-password").show();
    }
  });
});
li {
  list-style: none
}

.hide-password {
  display: none
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <input type="password" name="password" id="password" />
    <span class="password-showhide">
<span class="show-password">Show</span>
    <span class="hide-password">hide</span>
    </span>
    <li>
      <li>
        <input type="password" name="password" id="confirmPassword" />
        <span class="confirm-password-showhide">
<span class="show-password">Show</span>
        <span class="hide-password">hide</span>
        </span>
        <li>
</ul>



回答2:


you can use only one button to control.

  • js:

```

$('.showOrHide').click(function(e){
    var target = e.currentTarget
    $(target).hasClass('show')?hidePassword($(target)):showPassword($(target))
})
function hidePassword(e){
    e.removeClass('show').addClass('hide')
    e.prev('input').attr('type','password')
}
function showPassword(e){
    e.removeClass('hide').addClass('show')
    e.prev('input').attr('type','text')
}

html:

```

<input type="text" value="123456">
<button class="showOrHide show">click</button>

```

style:

```

.show{
    /*css you want */
    color: blue
}
.hide{
    /*css you want */
    color: red
}

```




回答3:


This is the shortest i think you can get

$(".confirm-password-showhide .trigger-password, .password-showhide .trigger-password").click(function() {
  var c = $(this).parent().attr("class").replace("-showhide", "");
  var obj = $("#" + (c.indexOf("confirm") > -1 ? "confirmPassword" : "password"));
  obj.attr("type", obj.attr("type") == "text" ? "password" : "text");
  $(this).text($(this).text() == "hide" ? "show" : "hide");
});

I've also removed the "hide" span from each of the passwords.

$(".confirm-password-showhide .trigger-password, .password-showhide .trigger-password").click(function() {
  var c = $(this).parent().attr("class").replace("-showhide", "");
  var obj = $("#" + (c.indexOf("confirm") > -1 ? "confirmPassword" : "password"));
  obj.attr("type", obj.attr("type") == "text" ? "password" : "text");
  $(this).text($(this).text() == "hide" ? "show" : "hide");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li><input type="password" name="password" id="password"> <span class="password-showhide"><span class="trigger-password">Show</span></span>
  </li>
  <li><input type="password" name="password" id="confirmPassword"> <span class="confirm-password-showhide"><span class="trigger-password">Show</span></span>
  </li>
</ul>



回答4:


show/Hide password function

function togglePassword($element){
    var newtype=$element.prop('type')=='password'?'text':'password';
    $element.prop('type',newtype);
}
$(document).ready(function() {
      $('#showUserNewPass').change(function(){
          togglePassword($('#userNewPass'));
          togglePassword($('#confirmNewPassword'));
      });
});



回答5:


<html>
    <head>
    <title>This Password Hide & Show</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
   </script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <style>
        #password
        {
            width: 50%;

        }
        #open
        {
            color: gray;
        }
        #closed
        {
            display: none;
            color: steelblue;
        }
        i
        {
            position: absolute;
            margin-top: -30px;
            margin-left: 280px;
        }
    </style>
     <script type="text/javascript">
         $(document).ready(function(){

             $("#open").click(function(){
                 $("#open").hide();
                 $("#closed").show();
                 $("#password").attr("type","text");
             });

             $("#closed").click(function(){
                 $("#closed").hide();
                 $("#open").show();
                 $("#password").attr("type","password");
             });

         });

</script>
</head>
<body>
    <h4>Password Hide & Show</h4>
    <input class="form-control" type="password" placeholder="Password" id="password" name="pwd">
    <i id="open" class="fa fa-eye fa-2x"></i>
    <i id="closed" class="fa fa-eye-slash fa-2x"></i>
</body>

</html>


来源:https://stackoverflow.com/questions/45561636/jquery-show-hide-password

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!