我想要創建表單字段提示,其中默認值顯示「密碼」並在焦點上,它會消失。如果該字段在沒有使用輸入的情況下失去焦點,它將顯示回默認值'密碼'。在密碼字段中禁用密碼掩碼
問題:密碼字段的默認值也被屏蔽。如何顯示「密碼」的默認值而不屏蔽,並且只屏蔽用戶輸入?
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>
+1 - 完全忘記了這一點。先生,幹得好。 – mrtsherman
Upvote用於標準,簡單的解決方案。但是,這不是完全向後兼容的,所以它不是很健壯。 – rockerest
是的,我認爲IE10 *最終*得到支持這個最令人敬畏的屬性。其他人已經擁有它了,嘆息.. –