2011-03-06 36 views
1

我有这样的代码:如何不提交水印文字?

$(document).ready(function() { 
    $('.watermark').focus(function() { 
     if (this.className == 'watermark') 
     { 
      this.className = ''; 
      this.reset = this.value; 
      this.value = ''; 
     } 
    }); 

    $('.watermark').blur(function() { 
     if (this.value == '') 
     { 
      this.className = 'watermark'; 
      this.value = this.reset; 
     } 
    }); 
}); 

,它工作正常,但是当我提交表单的水印提交数据。在不修改我的php处理文件的情况下,是否可以将带水印的文本提交为空白?我的意思是:如果文本框的类是“水印”,则为该文本框提交一个空白值。我不想修改我的php处理文件,但我不介意使用JQuery。最好的解决方案是捕获提交按钮的单击事件,并快速设置文本框的水印值为空?

+0

最好的解决方案不是通过首先设置*值*来完成您的水印。使用“占位符”或在(透明)输入下面放置标签。 – Quentin

+0

占位符是否完全支持?我无法让它工作。如何将标签放在输入之下?编辑:显然占位符文本只适用于Firefox 3.7+和我正在使用3.6我要升级,但我可能不应该使用它,如果这是新的。 –

+0

要在输入下放置标签,请使用绝对定位。未抛光的例子:http://dorward.me.uk/tmp/label-work/example.html对我来说 – Quentin

回答

0

如果我理解正确的话,你可以这样:

$('form').submit(function(){ 
    $(this).find('.watermark').each(function(){ 
     if(!this.reset) 
      this.value = ''; 
    }); 
}); 
0

使用placeholder属性上的文字输入元素:

<input type="text" placeholder="Text goes here" id="foo" /> 

因为这并不在一些较旧的浏览器,添加HTML5 placeholder属性后备脚本到您的页面(如thisthis)。