1

嵌套形式

<%= nested_form_for(@bill) do |f| %>

<p><%= f.link_to_add "Add Product", :bill_line_items %> </p>

部分账单行项目

<%= javascript_include_tag 'bill'%>

<%= f.hidden_field :bill_id %>

<%- prices = Hash[Product.all.map{|p| [p.id, p.price]}].to_json %>

<%= f.label :product_id %>
<%= f.collection_select :product_id ,Product.all,:id,:name, :class => 'product',  :prompt => "Select a Product", input_html: {data: {prices: prices}}%> <br/ >


<%= f.label :price, "price"%> 
<%= f.text_field :price, :size=>20, :class =>"price" %><br/>

bill.js

jQuery(document).ready(function(){
        jQuery('.product').change(function() {
            var product_id = jQuery(this).val();
            var price = eval(jQuery(this).data("prices"))[product_id];
            jQuery('.price').val(price);
        });
    });

Rails 3.2 问题:第二次单击添加产品并选择产品当我们选择第二个产品时,第二个产品的价格会覆盖第一个选定产品的价格。请求帮助。

4

1 回答 1

1

这是我得到的解决方案。我相信这是一种非常糟糕的方法,但它可以完成工作。

jQuery(document).ready(function(){
  jQuery("customer-outer").delegate("select", "change", function(event){
    var selectElement = jQuery(this);
    var productId = selectElement.val();
    var price = eval(selectElement.data('prices'))[productId];
    var lineItemWrapperElement = selectElement.parent().parent();
    jQuery("input.price", lineItemWrapperElement).val(price);
  });
});

customer-outer 是 div 定义的 new.html 和 edit.html 的类。如果有人为此找到适当的解决方案,请分享。

于 2012-08-01T03:59:05.270 回答