加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

html – 文本字段标签占位符返回值

发布时间:2020-12-14 21:25:11 所属栏目:资源 来源:网络整理
导读:我有一个表单,里面有一个text_field_tag。它是这样写的 %= text_field_tag "search",:placeholder = 'Enter search term...' % 但是,当HTML生成时,返回页面源… input id="search" name="search" type="text" value="{:placeholder=gt;quot;Enter search
我有一个表单,里面有一个text_field_tag。它是这样写的
<%= text_field_tag "search",:placeholder => 'Enter search term...' %>

但是,当HTML生成时,返回页面源…

<input id="search" name="search" type="text" value="{:placeholder=&gt;&quot;Enter search 
term...&quot;}" />

为什么这样做,我如何解决它,以便占位符工作?

解决方法

text_field_tag的第二个参数是值。给它没有空:
<%= text_field_tag "search",nil,:placeholder => 'Enter search term...' %>

并给它一个字符串以具有默认值:

<%= text_field_tag "search",'Enter search term...' %>

用jQuery添加一个onclick事件来清空它:

<%= text_field_tag "search",'Enter search term...',:onclick => 'if($(this).val()=="Enter search term..."){$(this).val("");};' %>

编辑2016:

现在,大多数浏览器现在都支持HTML 5 placeholder,这样我们可以用更干净的方法来实现:

<%= text_field_tag 'search',placeholder: 'Enter search term' %>
# which produces the following HTML:
<input type="text" value="" placeholder="Enter search term">

jsFiddle link

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读