2017-05-06 75 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