nested中具有每个link_to_add的唯一div ID

时间:2012-06-19 22:39:46

标签: javascript jquery ruby-on-rails nested-forms

## 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,因为我有一个javascript,用该collection_select的onchange事件填充“display”div。 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,以下代码将在您每次点击链接时创建唯一的div。

<强> HTML

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

​<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++;
    });
});

希望这有帮助!