Javascript 将html数据属性添加到简单表单输入
我正在尝试实现一个javascript插件,该插件要求我将data属性:data region id添加到select输入。我试着这样做,你需要的输入是:国家,倒数第三:Javascript 将html数据属性添加到简单表单输入,javascript,html,ruby-on-rails,ruby-on-rails-4,simple-form,Javascript,Html,Ruby On Rails,Ruby On Rails 4,Simple Form,我正在尝试实现一个javascript插件,该插件要求我将data属性:data region id添加到select输入。我试着这样做,你需要的输入是:国家,倒数第三: <div id="capdiv"> <h1>People#edit</h1> <p>Find me in app/views/people/edit.html.erb</p> <%= simple_form_for @person, u
<div id="capdiv">
<h1>People#edit</h1>
<p>Find me in app/views/people/edit.html.erb</p>
<%= simple_form_for @person, url: url_for(action:'update', controller:'people'), update: { success: "response", failure: "error"} do |f| %>
<div class="clearfix entity-add nested-fields person">
<div class="dynamic-container">
<div class="symegrid">
<div class="form-inline">
<%= f.input :fname, input_html: {class: 'fname form-input form-control fifty'}, label: "First Name" %>
<%= f.input :lname, input_html: {class: 'lname form-input form-control fifty'}, label: "Last Name" %>
</div>
<div class="form-inline">
<%= f.input :email, as: :email, input_html: {class: 'email form-input form-control'}, label: "Email" %>
<%= f.input :telephone, input_html: {class: 'telephone form-input form-control'}, label: "Telephone" %>
</div>
<div class="form-inline">
<%= f.input :street, input_html: {class: 'street form-input form-control'}, label: "Street" %>
<%= f.input :city, input_html: {class: 'city form-input form-control'}, label: "City" %>
</div>
<div class="form-inline"><%= f.input :country, as: :select, input_html: {data-region-id: "person_state", class: 'country form_control crs-country'}, label: "Country" %></div>
<div class="form-inline">
<%= f.input :state, as: :select, input_html: { id: "person_state", class: 'state form-control' }, label: "State/Province" %>
<%= f.input :zip, input_html: {class: 'zip form-input form-control'}, label: "Zip" %>
</div>
</div>
</div>
</div>
<% end %>
</div>
如何添加此属性?我在想,在最坏的情况下,我可以在页面加载后使用一些javascript强制加载,但这似乎是不必要的
提前感谢。问题是您不能在Ruby中将数据区域id声明为符号,破折号是无效字符。可以用符号表示字符串,如下所示: 数据区域id=>person\u状态 我认为Rails创建自定义数据属性的方法是嵌套它,如下所示:
数据:{region\u id:person\u state}Rails将把下划线转换成破折号这段时间我没有把数据放在input\u html中,所以很多文章让数据看起来像是一个顶级属性,当然还有其他帮助,但继承和魔力是疯狂的!。我船上的其他人,这就是它需要的样子:。这是为了得到一个与刺激JS/phew一起工作的复选框。
SyntaxError (/home/sam/Dropbox/railsproject/legal/app/views/people/edit.html.erb:20: syntax error, unexpected tLABEL
...t, input_html: {data-region-id: "person_state", class: 'coun...
... ^
/home/sam/Dropbox/railsproject/legal/app/views/people/edit.html.erb:20: syntax error, unexpected ',', expecting ')'
...data-region-id: "person_state", class: 'country form_control...
... ^
/home/sam/Dropbox/railsproject/legal/app/views/people/edit.html.erb:20: syntax error, unexpected '}', expecting ')'
...ntry form_control crs-country'}, label: "Country" );@output_...
... ^):
app/views/people/edit.html.erb:20: syntax error, unexpected tLABEL
app/views/people/edit.html.erb:20: syntax error, unexpected ',', expecting ')'
app/views/people/edit.html.erb:20: syntax error, unexpected '}', expecting ')'