2013-04-26 62 views
4

我想在我的密碼字段中顯示「密碼」的水印。

這裏是我的代碼:
的jQuery:如何將密碼字段中的文字水印?

$(document).ready(function() { 

    var watermark = 'Password'; 

    //init, set watermark text and class 
    $('#input_pwd').val(watermark).addClass('watermark'); 

    //if blur and no value inside, set watermark text and class again. 
    $('#input_pwd').blur(function(){ 
     if ($(this).val().length == 0){ 
      $(this).val(watermark).addClass('watermark'); 
     } 
    }); 

    //if focus and text is watermrk, set it to empty and remove the watermark class 
    $('#input_pwd').focus(function(){ 
     if ($(this).val() == watermark){ 
      $(this).val('').removeClass('watermark'); 
     } 
    }); 
}); 

HTML:

<input class="ui_input" id="input_pwd" type="password" name="pass" id="pass" style="height:25px; width:250px; font-size:14px;" required /> 

我的jsfiddle: click here


編輯:我喜歡jQuery的:)

編輯:我們的兄弟Konrad Gadzina給了我我正在尋找的東西,但是感謝所有人的努力!

+2

你的意思是 「佔位符」? – Raptor 2013-04-26 10:00:23

+2

https://github.com/Topener/jQuery-html5-placeholder – 2013-04-26 10:01:19

+0

在您進一步學習之前,請先了解水印和佔位符的定義 - 它們甚至幾乎沒有關係。 – George 2013-04-26 10:02:44

回答

5

使用html的placeholder

<input class="ui_input" id="input_pwd" type="password" name="pass" id="pass" style="height:25px; width:250px; font-size:14px;" placeholder='Password' required /> 

而且你不需要任何的js它。

注:Internet Explorer 10,Firefox,Opera,Chrome和Safari支持佔位符屬性。不幸的是,如評論所述,它不適用於較老的IE

+1

對於IE <9,你將需要JS仍然 – 2013-04-26 10:01:44

+0

你做,如果你想支持交叉瀏覽器(閱讀:IE) – 2013-04-26 10:01:45

+0

Mozilla Firefox支持嗎? – Tom 2013-04-26 10:02:15

0

如果要支持Internet Explorer,可以在輸入字段頂部使用浮動標籤。即,<input>標籤具有相應的<label>標籤,其在CSS中具有float: left,並且它們被定位爲使得標籤位於輸入字段的頂部。您還需要將.click()事件處理程序添加到激活輸入字段並隱藏標籤的標籤,並在分別隱藏和顯示標籤的輸入字段上添加.focus().blur()

如果您不需要支持Internet Explorer,<input>標記的placeholder屬性應該就足夠了。

0

爲什麼不能簡單的做

添加placeholder="your initial value"將解決我希望這個問題。

+0

這是HTML5屬性。諸如Firefox 3.x之類的舊瀏覽器將無法正常工作。 – Raptor 2013-04-26 10:18:22

2

您可以使用placeholder輸入屬性。

Username:<input type='text' placeholder="Username"/> <br/> 
Password: <input type='text' placeholder="Password"/> 

請參閱本jsFiddle Link

+1

我們可以看到,在這個SO問題中有很多downvotes,你能解釋爲什麼嗎?用戶明確沒有指定他使用的瀏覽器,我們只是基於我們對他的代碼上的jquery()片段的假設。 – KevinIsNowOnline 2013-04-26 10:05:58

8

您可以設置typetext默認情況下,並在除去watermark類其與JS改變password

HTML:

<input class="ui_input" id="input_pwd" type="text" name="pass" id="pass" style="height:25px; width:250px; font-size:14px;" required /> 

JS:

//if blur and no value inside, set watermark text and class again. 
    $('#input_pwd').blur(function(){ 
     if ($(this).val().length == 0){ 
      $(this).val(watermark).addClass('watermark'); 
      $(this).attr('type', 'text'); 
     } 
    }); 

    //if focus and text is watermrk, set it to empty and remove the watermark class 
    $('#input_pwd').focus(function(){ 
     if ($(this).val() == watermark){ 
      $(this).val('').removeClass('watermark'); 
      $(this).attr('type', 'password'); 
     } 
    }); 

檢查小提琴 - http://jsfiddle.net/w4Hh4/1/

+0

這不值得downvote ... – 2013-04-26 10:04:50

+0

工程就像一個魅力!多謝兄弟! – Tom 2013-04-26 10:08:12

+0

@Tom grat,我很高興它有幫助。如果此答案符合您的需求,請記住將其標記爲已接受的答案,以表明您的問題已解決。 – 2013-04-26 10:15:35

0

如果您不能使用HTML5,然後嘗試通過@每第一個答案建議切換模式Konrad Gadzina或試試這個小提琴:http://jsfiddle.net/w4Hh4/3/

將密碼框旁邊的文本框放在焦點和模糊處。

$(document).ready(function() { 

    var watermark = 'Password'; 

    //init, set watermark text and class 
     $('#input_pwd').hide(); 
     $('#input_pwd_fake').val(watermark).addClass('watermark'); 

//if blur and no value inside, set watermark text and class again. 
$('#input_pwd').blur(function(){ 

    if ($(this).val() == ''){ 
     $(this).hide(); 
     $('#input_pwd_fake').show().val(watermark).addClass('watermark'); 
    } 
}); 

//if focus and text is watermrk, set it to empty and remove the watermark class 
$('#input_pwd_fake').focus(function(){ 
    $(this).hide(); 
    $('#input_pwd').show().focus(); 
}); 
}); 
+0

如果您不滿意,請儘可能告訴我們爲什麼 – Yeronimo 2013-04-26 10:21:40

0

這將工作的跨瀏覽器,因爲它是一個簡單的JavaScript來檢查值設置,等於默認設定值或者聚焦狀態或的onblur

<input type="password" onfocus="if(this.value=='Password') {this.value='';}" onblur="if(this.value=='') {this.value='Password'}" value="Password" /> 
0

在CSS

input.watermark { 
    color: #D3D3D3; font-weight: 400; 
} 

的jQuery設置一個類水印: -

$(document).ready(function() { 

    var watermark = ''; // watermark value 

    $('#myID').val(watermark).addClass('watermark'); 

    $('#myID').blur(function(){ 
     if ($(this).val().length == 0){ 
      $(this).val(watermark).addClass('watermark'); 
     } 
    }); 

    $('#input_fname').focus(function(){ 
     if ($(this).val() == watermark){ 
      $(this).val('').removeClass('watermark'); 
     } 
    }); 
});