2011-11-11 74 views
2

我想要創建表單字段提示,其中默認值顯示「密碼」並在焦點上,它會消失。如果該字段在沒有使用輸入的情況下失去焦點,它將顯示回默認值'密碼'。在密碼字段中禁用密碼掩碼

問題:密碼字段的默認值也被屏蔽。如何顯示「密碼」的默認值而不屏蔽,並且只屏蔽用戶輸入?

jQuery代碼

$(".splash_register_short_input, .splash_register_long_input").each(function() { 
    $(this).val($(this).attr('title')); 
}); 

$(".splash_register_short_input, .splash_register_long_input").focus(function() { 
    if($(this).val() == $(this).attr('title')) { 
     $(this).val(''); 
    } 
}); 

$(".splash_register_short_input, .splash_register_long_input").blur(function() { 
    if($(this).val() == '') { // If there is no user input 
     $(this).val($(this).attr('title')); 
     $(this).removeClass('splash_register_have_userinput'); 
    } else { // If there is user input 
     $(this).addClass('splash_register_have_userinput'); 
    } 
}); 

HTML代碼

<form id="splash_register_traditional_form"> 
    <input type="text" name="register_first_name" class="splash_register_short_input" title="First Name" /><label for="register_first_name"></label> 
    <input type="text" name="register_last_name" class="splash_register_short_input" title="Last Name" /><label for="register_last_name"></label> 
    <input type="text" name="register_email" class="splash_register_long_input" title="Email" /><label for="register_email"></label> 
    <input type="password" name="register_password" class="splash_register_long_input" title="Password" /><label for="register_password"></label> 
    <input type="submit" id="splash_register_signup_button" value="Sign up" /> 
</form> 

回答

6

您可以簡單地使用HTML5 placeholder -attribute:

<input type="password" placeholder="Enter Password..." /> 

關於在評論中提到的缺乏向後兼容性,this jQuery placeholder plugin與某種組合如果屬性不被支持(如可能this one)可能會有所幫助。

+2

+1 - 完全忘記了這一點。先生,幹得好。 – mrtsherman

+0

Upvote用於標準,簡單的解決方案。但是,這不是完全向後兼容的,所以它不是很健壯。 – rockerest

+0

是的,我認爲IE10 *最終*得到支持這個最令人敬畏的屬性。其他人已經擁有它了,嘆息.. –

0

要以編程方式隱藏密碼,請在下面添加以下代碼oncreate android.provider.Settings.System.putInt(this.getContentResolver(),android.provider.Settings.System.TEXT_SHOW_PASSWORD,0);