开发者

Setting hint text in a text field in Ruby on Rails

Can some one suggest the best way for setting hint text(not default text) for a text field in Ruby on Rails. Currently I am using this:

<%= 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';}",
    :onblu开发者_高级运维r => "if(this.getValue()==''){this.setValue('last, first');this.style.color = #aaa';}" 
  } %>

The text field is attached to a model and in turn to a value in a database. Also, the same html that is used in index.html is also used in edit.html and hence when I try to edit the field, the default value shows up instead of the value from the database. What is the correct way to do this?

Note: I am not trying to set a default value but just a hint to what needs to be entered in the text box.

Thanks,

Raja Ram


I'd recommend using the HTML 5 placeholder attribute, and then using a jQuery plugin to make it work on older browsers (like this one, but there are many others on Google).

You can see this technique used in production here.


Try this jquery plugin

http://plugins.jquery.com/project/hint

or choose here:

http://plugins.jquery.com/plugin-tags/hint

For prototype

http://davidchambersdesign.com/autopopulating-input-fields-with-prototype/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜