【发布时间】:2018-07-27 07:14:36
【问题描述】:
我需要在点击眼睛图标时显示和隐藏用户密码,所以我为此编写了脚本,当我点击眼睛图标时,只有类在改变,但密码不可见,再次点击斜线图标应该隐藏了这两种方法都不起作用如何解决这个问题?
<input type="password" name="player_password" id="pass_log_id" />
<span toggle="#password-field" class="fa fa-fw fa-eye field_icon toggle-password"></span>
<script>
$("body").on('click','.toggle-password',function(){
$(this).toggleClass("fa-eye fa-eye-slash");
var input = $("#pass_log_id").attr("type");
if (input.attr("type") === "password") {
input.attr("type", "text");
} else {
input.attr("type", "password");
}
});
</script>
【问题讨论】:
-
这里有一个工作代码示例codepen.io/Sohail05/pen/yOpeBm
-
@Manasi 请告诉我我的代码有什么问题
-
@MSp 你在做 ** ".attr("type");" 2次。 ** 1) var 输入 = $("#pass_log_id").attr("type");和 2) if (input.attr("type") === "password")
标签: javascript jquery