瀏覽器基於安全考量,type="password" 的欄位本來就不該顯示明文,所以「未輸入時顯示提示文字、開始打字後變成圓點」這種效果,不能用單一個密碼框硬做。實務上的做法是準備兩個輸入框:一個 text 當提示用的假欄位,一個 password 當真正的輸入框,兩者樣式與定位完全重疊,再依狀態切換顯示。
運作流程其實只有三步:
text 框,value 放「請輸入您的密碼」。password 框。password 框失去焦點時若內容為空,就切回 text 框。<table class="login_table"> <tr> <td>帳號</td> <td><input type="text" value="請輸入您的帳號" class="admin" /></td> </tr> <tr> <td>密碼</td> <td> <input type="text" value="請輸入您的密碼" id="login_showPwd" /> <input type="password" id="login_password" style="display: none" /> </td> </tr> <tr> <td> <input type="button" value="登錄" /> <input type="button" value="重置" id="btn_res" /> </td> </tr> </table>
// 帳號欄位:取得焦點時清空提示,離開時補回提示
$(function(){
$(".admin").focus(function(){
if($(this).val() == "請輸入您的帳號"){ $(this).val(""); }
});
$(".admin").blur(function(){
if($(this).val() == ""){ $(this).val("請輸入您的帳號"); }
});
// 密碼欄位:假欄位取得焦點 → 換真欄位
$('#login_showPwd').focus(function(){
var text_value = $(this).val();
if(text_value == this.defaultValue){
$('#login_showPwd').hide();
$('#login_password').show().focus();
}
});
// 真欄位離開且為空 → 換回假欄位
$('#login_password').blur(function(){
var text_value = $(this).val();
if(text_value == ""){
$('#login_showPwd').show();
$('#login_password').hide();
}
});
// 重置
$('#btn_res').click(function(){
$('.admin').val("請輸入您的帳號");
$('#login_password').hide().val("");
$("#login_showPwd").show();
});
});
如果你只是想讓密碼框裡出現一行灰字提示,根本不需要兩個輸入框——直接用 HTML5 的 placeholder 就好,瀏覽器會自動在該欄位是 password 時把提示文字以明文顯示,開始輸入後自然消失:
<input type="password" name="pas" placeholder="密碼" />
這一點很多人會誤以為提示字必須用 text 框才做得到,其實關鍵屬性就是 placeholder。

兩種方式取決於你的情境:需要「提示字 + 自訂樣式 + 額外互動」時用雙框切換;只是單純提示,一行 placeholder 就足夠,程式碼更少也更好維護。