Simple Form: Is it possible to remove labels and replace them with placeholders using the labels i18n? [duplicate]

江枫思渺然 提交于 2019-12-10 01:09:52

问题


I'm trying to produce a regular simpleform form

= simple_form_for [:hq, @team, @order] do |f|
  .tickets
    = f.select :tickets, :aaa => "aaa"
    = render "ticket"


  .details
    .left
      = f.input :address1
      = f.input :address2
      = f.input :state
      = f.input :country
      = f.input :email
    .right
      = f.input :comments

But I want the inputs to not be rendered with a label, just the title "Address Line 1" in the placeholder.

I know I can do this with f.input :address1, :placeholder => "whatever", :label => "" but I'd like it to be configurable with a wrapper and take the i18n value form label, not placeholder.


回答1:


Try doing the following:

f.input :address1, placeholder: "Address Line 1", label: false



回答2:


If you want to use javascript, here is a jQuery snippet that does the job:

$.each($(".left .input"), function() {
  var label = $("label",$(this)).text();
  $("label",$(this)).hide();
  $("input",$(this)).attr("placeholder",label);
});

The main advantage using js for this case is that the result remains screen-readers compliant (they will read the label because they won't run the js).



来源:https://stackoverflow.com/questions/12558957/simple-form-is-it-possible-to-remove-labels-and-replace-them-with-placeholders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!