删除按钮提交时输入文本的默认值

时间:2012-12-09 23:18:58

标签: html

如果用户不填写包含信息的字段,我需要知道如何删除输入文本的默认值。现在,当搜索栏显示时,我的表单显示“eg:Ipod,Iphone”。代码示例:

<input type="text" value="eg: Ipod, Iphone" name="Product" id="Product"
onblur="if (this.value == '') {this.value = 'eg: Ipod, Iphone';}"
onfocus="if (this.value == 'eg: Ipod, Iphone') {this.value = '';}" />
<button class="submit" title="Post" type="submit"><span>Submit</span></button>

我的想法是显示该信息 - 例如:Ipod,Iphone - 但如果有人按下提交但该文本框中没有新信息,则传递的信息将更改为Ipod或Iphone,而不是例如:Ipod,Iphone等。提前谢谢。

AAAA

1 个答案:

答案 0 :(得分:3)

如评论中所述,使用html属性placeholder

<input type="text" placeholder="eg: Ipod, Iphone" /> 

对于不支持此属性的旧浏览器,有一些可以处理此问题的jquery插件。我们使用jquery-watermark。它只需要您在输入上设置class="watermark"并包含脚本文件。