我有下面這段jQuery代碼:插入默認值
$(".SearchForm input:text").each(function(){
/* Sets the current value as the defaultvalue attribute */
if(allowedDefaults.indexOf($(this).val()) > 0 || $(this).val() == "")
{
$(this).attr("defaultvalue", $(this).val());
$(this).css("color","#9d9d9d");
/* Onfocus, if default value clear the field */
$(this).focus(function(){
if($(this).val() == $(this).attr("defaultvalue"))
{
$(this).val("");
$(this).css("color","#4c4c4c");
}
});
/* Onblur, if empty, insert defaultvalue */
$(this).blur(function(){
alert("ud");
if($(this).val() == "")
{
$(this).val($(this).attr("defaultvalue"));
$(this).css("color","#9d9d9d");
}else
{
$(this).removeClass("ignore");
}
});
}
});
我用這個代碼中插入一些默認的文本到我的一些輸入字段,當沒有其它的類型這意味着當用戶看到我的搜索表單時,默認值將被設置爲輸入字段上的一個屬性,並且這將是顯示的值。當用戶在輸入欄內單擊時,默認值將被刪除。
當用戶看到的輸入字段起初是看起來像這樣:
<input type="text" value="" defaultvalue="From" />
這只是正常的,但我有一個很大的挑戰。如果用戶發佈了表單,並且某個字段被輸入了某個字段,那麼如果用戶從輸入字段中刪除了文本,則無法在該字段中顯示默認值。 發生這種情況是因爲即使用戶刪除內容,文本字段的值仍包含某些內容。所以我的問題是如何在提交表單時顯示默認值,然後用戶刪除輸入的內容?
當表單提交的輸入是這樣的,並且繼續尋找下去,直到形式再次提交:
<input type="text" value="someValue" defaultvalue="From" />
所以我需要右後顯示在輸入字段的默認值用戶已經刪除了該字段中的內容,並從字段中刪除了該焦點。
大家都明白我的問題是什麼嗎?否則,只要問一下,我現在已經在這個問題上掙扎了很長時間了,所以任何幫助都將不勝感激。
由於提前, 金安徒生