【问题标题】:Setting hint text in a text field in Ruby on Rails在 Ruby on Rails 的文本字段中设置提示文本
【发布时间】:2011-07-01 14:44:56
【问题描述】:

有人可以建议在 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,因此当我尝试编辑该字段时,会显示默认值而不是数据库中的值。这样做的正确方法是什么?

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

谢谢,

拉贾拉姆

【问题讨论】:

    标签: ruby-on-rails textfield hint


    【解决方案1】:

    【讨论】:

    • 感谢您的回复。但是原型库中是否有类似的东西?我目前正在使用原型库。
    【解决方案2】:

    我建议使用 HTML 5 placeholder 属性,然后使用 jQuery 插件使其在较旧的浏览器上工作(如 this one,但有 are many others on Google)。

    你可以see this technique used in production here

    【讨论】:

    • +1 for HTML5 :) 实际上大多数插件都通过占位符属性工作
    猜你喜欢
    • 2012-12-19
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多