我需要在单击眼睛图标时显示和隐藏用户密码,所以我为此编写了脚本,当我单击眼睛图标时,只有类正在更改但密码不可见,再次单击斜线眼睛图标它应该隐藏这两个方法不起作用如何解决这个问题?
<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>
原文由 Anonymous 发布,翻译遵循 CC BY-SA 4.0 许可协议
你的
input
实际上是字符串。检查控制台,您应该看到字符串没有方法attr()
因为您将$().attr()
分配给input