有一种方法可以从按钮动态输入输入吗? 例如:
这是表格:
<%= form_for(@order) do |f| %>
。 ...
<div class="field">
<%= f.label :productlist %><br>
<%= f.select :productlist, @products, :prompt => 'Select one!' %>
<%= link_to 'Add More', '#', id: 'products-link' %>
</div>
<div id="newproduct">
<p>load here the new input on Add more link</p>
</div>
<div class="field">
<%= f.label :status %><br>
<%= f.text_field :status %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
所以我想到了ajax和一种将部分输入加载到表单中的方法,但我不明白正确的方法来做到这一点。
谢谢
答案 0 :(得分:0)
在您的javascript文件中添加:
$("a#products-link").click(function(){
$.get('/partials/products_form', function(data){
$("div#newproduct").append(data);
}, 'html');
});
基本上发生的事情是,您将通过ajax将get
请求发送到/partials/products_form
中的表单或您选择保留表单部分的位置。然后,此html将附加到您在此案例中选择的div /标识符div#newproduct
。