2017-05-06 43 views
0
<p class="input-group pwdField"> 
    <input type="password" name="password" placeholder="Enter your Password" required> 
    <i class="fa fa-eye"></i> 
</p> 

这是我的HTML标记和我的jQuery代码如下。为什么我的JQuery切换密码可见性不起作用?

$(function() { 

    $(".input-group").children("i").click(function() { 

     $(this).toggleClass("fa-eye fa-eye-slash"); 

     var inputField = $(".pwdField").children().first(); 

     if ($(inputField).attr("type", "password")) { 

      $(inputField).attr("type", "text"); 

     } else { 
      $(inputField).attr("type", "password"); 
     } 

    }); 

}); // End of document.ready(); 

请帮助我,当我点击的眼睛图标在输入字段,everythng是可以正常使用,但是当我再次点击,输入字段并没有改变自己的属性后面键入从键入e =“密码” =“文本”

回答

0

您是设置if()越来越里面的属性,所以你可以检查它的价值

而是尝试做这样的事情:

// get the attribute value 
var type = $(inputField).attr("type"); 
// now test it's value 
if(type === 'password'){ 
    $(inputField).attr("type", "text"); 
}else{ 
    $(inputField).attr("type", "password"); 
} 

注意区别......你设置提供了第二个参数时和得到时,它不是

+0

呀。在我发布问题后得到它。但感谢你的宝贵答案。 – srithan