记住原始值的输入框

Avatar of Chris Coyier
Chris Coyier
var origValue = [];
$('input.remember').each ( function (currentIndex)
{
       origValue.push ( $(this).val () );
       $(this).focus ( function ()
       {
               $(this).removeClass("unfocused");
               var defaultText = $(this).val();
               if ( $(this).val () == origValue [ currentIndex ] )
               {
                       $(this).val('');
               }

               $(this).blur(function()
               {
                       var userInput = $(this).val();
                       if (userInput == '')
                       {
                               $(this).val(defaultText);
                               $(this).addClass("unfocused");
                       }
               });
       });
});

一个jQuery代码片段,用于使表单输入显示帮助消息,点击时消失(用户未输入任何内容时重新出现)。为您的输入添加“remember”类以激活代码片段,并(可选)添加“unfocused”类作为CSS钩子,用于更改帮助消息的样式。