我强烈反对显示password as the user typing it. 相反,您可以实现一种通过用户操作显示字段状态的方法,如user click 显示密码或打开hover.原因是用户决定显示密码。此外,提高安全性,因为用户在键入时可能并不孤单
例如:
代码来自herdiansc
//Place this plugin snippet into another file in your applicationb
(function ($) {
$.toggleShowPassword = function (options) {
var settings = $.extend({
field: "#password",
control: "#toggle_show_password",
}, options);
var control = $(settings.control);
var field = $(settings.field)
control.bind(\'click\', function () {
if (control.is(\':checked\')) {
field.attr(\'type\', \'text\');
} else {
field.attr(\'type\', \'password\');
}
})
};
}(jQuery));
//Here how to call above plugin from everywhere in your application document body
$.toggleShowPassword({
field: \'#test1\',
control: \'#test2\'
});
jsfiddle
和用户
Baraka Mahili$(\'#show_password\').hover(function functionName() {
//Change the attribute to text
$(\'#password\').attr(\'type\', \'text\');
$(\'.glyphicon\').removeClass(\'glyphicon-eye-open\').addClass(\'glyphicon-eye-close\');
}, function () {
//Change the attribute back to password
$(\'#password\').attr(\'type\', \'password\');
$(\'.glyphicon\').removeClass(\'glyphicon-eye-close\').addClass(\'glyphicon-eye-open\');
}
);
https://codepen.io/GBMahili/pen/pEvVZP