change type of input field with jQuery

后端 未结 29 2141
青春惊慌失措
青春惊慌失措 2020-11-22 05:13
$(document).ready(function() {
    // #login-box password field
    $(\'#password\').attr(\'type\', \'text\');
    $(\'#passwo         


        
29条回答
  •  花落未央
    2020-11-22 05:50

    Here is a method which uses an image next to the password field to toggle between seeing the password (text input) and not seeing it (password input). I use an "open eye" and "closed eye" image, but you can use whatever suits you. The way it works is having two inputs/images and upon clicking the image, the value is copied from the visible input to the hidden one, and then their visibility is swapped. Unlike many of the other answers which use hardcoded names, this one is general enough to use it multiple times on a page. It also degrades gracefully if JavaScript is unavailable.

    Here is what two of these look like on a page. In this example, the Password-A has been revealed by clicking on its eye.

    $(document).ready(function() {
      $('img.eye').show();
      $('span.pnt').on('click', 'img', function() {
        var self = $(this);
        var myinp = self.prev();
        var myspan = self.parent();
        var mypnt = myspan.parent();
        var otspan = mypnt.children().not(myspan);
        var otinp = otspan.children().first();
        otinp.val(myinp.val());
        myspan.hide();
        otspan.show();
      });
    });
    img.eye {
      vertical-align: middle;
    }
    
    
    
    Password-A: *
    Password-B: *

提交回复
热议问题