在Ruby on Rails中的文本字段中设置提示文本

时间:2011-02-21 22:26:14

标签: ruby-on-rails textfield hint

有人可以建议在Ruby on Rails中为文本字段设置提示文本(不是默认文本)的最佳方法。目前我正在使用这个:

<%= text_field_with_auto_complete
  "customer", 
  :contact_person, { 
    :value => 'last, first',
    :style => 'color:#aaa;width:11em;', 
    :onfocus => "if(this.getValue()=='last, first'){this.clear();this.style.color = '#000';}",
    :onblur => "if(this.getValue()==''){this.setValue('last, first');this.style.color = #aaa';}" 
  } %>

文本字段附加到模型,然后附加到数据库中的值。此外,index.html中使用的相同html也在edit.html中使用,因此当我尝试编辑该字段时,默认值显示而不是数据库中的值。这样做的正确方法是什么?

注意:我不是要设置默认值,而只是提示需要在文本框中输入的内容。

谢谢,

拉贾拉姆

2 个答案:

答案 0 :(得分:4)

我建议使用HTML 5 placeholder属性,然后使用jQuery插件使其适用于旧浏览器(例如this one,但are many others on Google)。

你可以see this technique used in production here

答案 1 :(得分:2)