Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 如何将占位符添加到文本\字段\标记?_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 4 如何将占位符添加到文本\字段\标记?

Ruby on rails 4 如何将占位符添加到文本\字段\标记?,ruby-on-rails-4,Ruby On Rails 4,我的表格中有以下行: = text_field_tag :mother_name, nil, id: 'mother_name_autocomplete', class: 'form-group form-control', autocomplete: 'off', data: {provide: 'typeahead'} 如何将placehodler添加到此字段?如果rails>=3.0,您只需使用占位符选项即可 f.text_field :mother_name, :placeholder

我的表格中有以下行:

= text_field_tag :mother_name, nil, id: 'mother_name_autocomplete', class: 'form-group form-control', autocomplete: 'off', data: {provide: 'typeahead'}

如何将placehodler添加到此字段?

如果
rails>=3.0
,您只需使用
占位符
选项即可

f.text_field :mother_name, :placeholder => "mother_name_autocomplete"

您可以在这里找到更多参考资料:

如果您使用的是
文本\u字段\u标签
,那么您可以这样做

<%= text_field_tag :mother_name, nil, placeholder: 'Some text' %>

请注意,第二个参数是
nil


<%= text_field_tag "search", 'Enter search term...' %>
添加onclick事件以使用jQuery清空它:

<%= text_field_tag "search", 'Enter search term...', :onclick => 'if($(this).val()=="Enter search term..."){$(this).val("");};' %>
“如果($(this.val()==”输入搜索词…){$(this.val(“”;};”””%>
参考资料:-

例如,id=“input_name”,value=nil,placeholder=“输入您的姓名”

因此,提供的标签如下所示:

 <%= text_field_tag 'input_name', nil, placeholder: "Enter your name" %>

上述代码将按如下所述生成

<input type="text" name="input_name" id="input_name" placeholder="Enter your name">

请在此处阅读有关标签的更多信息:

现在占位符作为DOM属性得到了正确的支持,完全没有必要这样做。