【发布时间】:2014-01-15 00:49:10
【问题描述】:
我有一个简单的表格,有 5 个问题(文本区域)供用户填写。我希望用户能够通过单击“加号”按钮添加额外的问题。
最简单的方法是什么?请我在包括 jQuery 在内的 JavaScript 方面不是那么强,所以我会请求一些清晰的代码,如果可能的话,请很少解释。
这是我的看法:
<% question_numbering = 0 %>
<%= simple_form_for(@quiz, html: {class: 'form-vertical', id: 'someform' }) do |f| %>
<div>
question <%= question_numbering += 1 %><br>
<%= f.input_field :content, :rows => 3, :style => "width:80%", :placeholder => "enter your question." %><br>
question <%= question_numbering += 1 %><br>
<%= f.input_field :content, :rows => 3, :style => "width:80%", :placeholder => "enter your question." %><br>
question <%= question_numbering += 1 %><br>
<%= f.input_field :content, :rows => 3, :style => "width:80%", :placeholder => "enter your question." %><br>
question <%= question_numbering += 1 %><br>
<%= f.input_field :content, :rows => 3, :style => "width:80%", :placeholder => "enter your question." %><br>
question <%= question_numbering += 1 %><br>
<%= f.input_field :content, :placeholder => "enter your question." %><br>
</div>
<button type="button" class="btn btn-default" id = "js-add-question-row">
<span class="glyphicon glyphicon-plus"></span>
</button>
<%= f.submit 'Submit', :class => "btn btn-default" %>
<% end %>
请注意question <%= question_numbering += 1 %> 仅用于对表格中的问题进行编号。
【问题讨论】:
-
为了其他可能希望解决相同问题的人,请查看这些 rails-casts:196-nested-model-form-part-1197-nested-model-form-part-2。希望他们有所帮助。
标签: javascript jquery ruby-on-rails ruby-on-rails-4 erb