2011-03-03 48 views
2

我的「標籤」是輸入裏面,所以我想清空現場,當用戶點擊,如果其空「標籤」又回來了標籤使用jQuery

 $('#name').focus(function() { 
     if ($(this).val()=="Your name") { $(this).val(""); } 
    }).blur(function() { 
     if ($(this).val()=="") { $(this).val("Your name") } 
    }); 

,這將是完成了3個或更多的領域,還有更好的選擇?無需使用插件,只需jQuery的

+0

您還需要考慮,如果他們離開該領域的空白,並提交形式會發生什麼。 – 2011-03-03 19:33:49

+0

其ajax答覆,我做php驗證只在這種情況下 – braindamage 2011-03-03 19:38:30

+0

http://stackoverflow.com/questions/5120257/what-is-the-best-html5-placeholder-like-jquery-plugin-out-there/5120428 #5120428 – McHerbie 2011-03-03 19:47:27

回答

2

如果你想讓它與其他領域的工作,你很容易可以刪除檢查,並把在外地的title屬性的值Your name

$('.waterMarkInput').focus(function() { 
    if ($(this).val()==$(this).attr("title")) { $(this).val(""); } 
}).blur(function() { 
    if ($(this).val()=="") { $(this).val($(this).attr("title")); } 
}); 

然後你輸入域看起來是這樣的:

<input type="text" value="" class="waterMarkInput" title="Your name"/> 
+0

謝謝! ($(this).val()==「」){$(this).val($(this).attr(「title」)); } – braindamage 2011-03-03 19:42:22

+0

糟糕,固定。 ':'' – 2011-03-03 19:43:46

+1

我開始使用這個腳本,並增加了簡單地使用字段當前值的功能,所以不需要具有title屬性。還添加了必要的延期執行。

 $(document).ready(function(){ \t $('.inlineLabel').focus(function() { \t \t if ($(this).attr("initial") == undefined) { \t \t \t $(this).attr("initial",$(this).val()); \t \t } \t \t if ($(this).val() == $(this).attr("initial")) { \t \t \t $(this).val(""); \t \t } \t }).blur(function() { \t \t if ($(this).val() == "") { \t \t \t $(this).val($(this).attr("initial")); \t \t } \t }); }); 
xamde 2011-03-15 20:54:20