Reputation: 27
I have been struggling to learn jQuery lately. I have finally got jQuery to total my form data at the bottom, but I need to add another piece to the puzzle.
Currently it takes the amount the end user inputs and multiplies the total by 4 which is the price of the apples. I need to make it so the form will reduce the amount by $2 if they order 3 or more apples. (Buy 1 apple for $4 or 3 for $10)
Live version: http://jsfiddle.net/sbdthru/cSfmy/
var $form = $('#apple-order'),
$summands = $form.find('.apple'),
$sumDisplay = $('#total');
$form.delegate('.apple', 'change', function ()
{
var sum = 0;
$summands.each(function ()
{
var value = Number($(this).val());
if (!isNaN(value)) sum += value;
});
$sumDisplay.text(sum * 4);
});
Upvotes: 0
Views: 59
Reputation: 3947
Add a modifier that tests the sum:
var modifier = sum >= 3 ? 2 : 0;
$sumDisplay.text(sum * 4 - modifier);
Upvotes: 3