0

我有一个名为“Size”的选择框集合,其值为 10、25、50 等。我可以使用 jquery 添加/删除此字段,例如,页面上可能有 3 种不同的尺寸选择:25、 50 和 10。如果我想在这个页面上总结这些值,得到 85 的总数,我该怎么做呢?

观众.rb

SIZE = ['5', '10', '25', '50', '75', '100']

form.html.erb

<%= f.fields_for :audiences do |audience_form| %>
  <div class="audiencefields">
  <span class="audienceforminsert"></span>  
  <div>
    <%= audience_form.label :number_of_people, "Size" %><br />
    <%= audience_form.collection_select :number_of_people, Audience::SIZE, :to_s, :to_s, :include_blank => true %>
  </div>

  </div>

  <%= audience_form.link_to_remove "Remove this audience", :id => "removelink" %>
  <% end %>

  <p><%= f.link_to_add "Add another audience", :audiences, :id => "addlink" %></p>

应用程序.js

$("#removelink").hide().filter(":first-child").show();

$('form a.add_nested_fields, form a.remove_nested_fields').live('click', function(){
$("div.audiencefields span.audienceforminsert").each(function(index, element) {
    //index starts with 0
    $(this).text("Audience");});
});

$("span.audienceshowinsert").each(function(index, element) {
    //index starts with 0
    $(this).text("Audience " + (index + 1));
});
4

1 回答 1

0

找到所有选择框并添加它们的选定值:

var combined = 0;
$("select").each(function() {
    combined += parseInt($(this).val());
});

这是一个示例jsFiddle

于 2012-07-02T20:14:04.493 回答