0
## NO-AJAX, No filtering
# Since there is no filtering, it lists all the orders.
##
# partial:
<%= f.collection_select(:customer, @customers,
  :customer_id, :customer_name, {:prompt=>'Select a Customer'})
%>
<%= f.collection_select(:order_id, @orders,
  :order_id, :order_name, {:prompt=>'Select an Order'} )
%>

# view:
<%= nested_form_for @service do |f| %>
  <%= f.link_to_add "Add Order", :orders %>
  <%= f.submit "Submit" %>
<% end %>

#controller:
def new
  @service = Service.new
  @service.orders.build
  @customers = Customer.find(:all)
  @orders = Order.find(:all)
end


## Q: BUT I WANT TO DO filtering based on the customer select, with AJAX and update the  div accordingly.  
# partial:
<%= f.collection_select(:customer, @customers,
  :customer_id, :customer_name, {:prompt=>'Select a Customer'})%>
<div id='display'></div>

これが私の質問です: 各 link_to_add で、一意の「表示」div を作成したいと思います。これは、その「表示」div にその collection_select の onchange イベントを設定する JavaScript があるためです。助けてくれてありがとう!

4

1 に答える 1

0

あなたの質問を正しく理解できれば、次のコードは、リンクをクリックするたびに一意の div を作成します。

HTML : </p>

<a href="#" id="link_to_add">Link to Add</a>

​&lt;div id="parent_div">
</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

jQuery :

var divCnt = 1;
$(document).ready(function() {
    $('#link_to_add').click(function(){
        $('#parent_div').html($('#parent_div').html()+'<div id="display_'+divCnt+'">Display div '+divCnt+'</div>');
        divCnt++;
    });
});

お役に立てれば!</p>

于 2012-06-19T23:13:17.230 に答える