仍在努力解决这个难题。我有一个更完整的例子here。该模型称为 PanOrder。
这是我在 /assets/javascripts/pan_orders.js 中的内容:
function pans_total_cost ()
{
var p;
var individual_price;
var n;
var pans;
p = document.getElementById("price_per_pan");
individual_price = p.value;
n = document.getElementById("number_of_pans");
pans = n.value;
if (pans > 0)
{
document.getElementById.html("pans_total").value = n * p;
}
}
...这就是我的 /views/pan_orders/_form.html.erb 中的内容
<%= form_for(@pan_order) do |f| %>
<% if @pan_order.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@pan_order.errors.count, "error") %> prohibited this pan_order from being saved:</h2>
<ul>
<% @pan_order.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :price_per_pan, :value => 300 %>
<%= f.hidden_field :extra_shipping, :value => 25 %>
<div class="field">
<%= select_tag :number_of_pans, options_for_select(pans_array)%>
</div>
<h3>Subtotal: <span id="pans_total"></span></h3>
<div class="field">
<%= check_box_tag :expedited_shipping, :checked => false, :onclick => "pans_total_cost()" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
...这就是我的 /helpers/pan_orders_helper.rb 中的内容
module PanOrdersHelper
def pans_array
numbers = []
zero = ["0", 0]
one = ["1", 1]
two = ["2", 2]
three = ["3", 3]
four = ["4", 4]
five = ["5", 5]
numbers << zero << one << two << three << four << five
end
end
但是,当显示表单时,我从下拉列表中选择了 1 个或多个平底锅,然后单击复选框,“小计”后没有任何内容出现。