jquery - Rails 3: How to sum multiple collection selections? -
i have select box collection called "size", values of 10, 25, 50, etc. can add/remove field using jquery that, example, there 3 different size selections on page: 25, 50, , 10. if want sum these values on page, , total of 85, how can accomplish this?
audience.rb
size = ['5', '10', '25', '50', '75', '100']
form.html.erb
<%= f.fields_for :audiences |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 audience", :id => "removelink" %> <% end %> <p><%= f.link_to_add "add audience", :audiences, :id => "addlink" %></p>
application.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 0 $(this).text("audience");}); }); $("span.audienceshowinsert").each(function(index, element) { //index starts 0 $(this).text("audience " + (index + 1)); });
find of select boxes , add selected value:
var combined = 0; $("select").each(function() { combined += parseint($(this).val()); });
here example jsfiddle.
Comments
Post a Comment