I'm jQuery's newbie so I go by trial and ask the more experienced help.
This is the part of the code
click: function () { //Click event
if (this.status() == 'available') { //optional seat
var maxSeats = 3;
var ms = sc.find('selected').length;
//alert(ms);
if (ms < maxSeats) {
price = this.settings.data.price;
/*
$('<option selected>R'+(this.settings.row+1)+' S'+this.settings.label+'</option>')
.attr('id', 'cart-item-'+this.settings.id)
.attr('value', (this.settings.row+1)+'_'+this.settings.label)
.data('seatId', this.settings.id)
.appendTo($cart);
*/
$('<option selected>R'+(this.settings.row+1)+' S'+this.settings.label+'</option>')
.attr('id', 'cart-item-'+this.settings.id)
.attr('value', this.settings.id)
.attr('alt', price)
.data('seatId', this.settings.id)
.appendTo($cart);
$counter.text(sc.find('selected').length+1);
$counter.attr('value', sc.find('selected').length+1);
$total.text(recalculateTotal(sc));
$total.attr('value', recalculateTotal(sc));
return 'selected';
}
alert('You can only choose '+ maxSeats + ' seats.');
return 'available';
} else if (this.status() == 'selected') { //Checked
//Update Number
$counter.text(sc.find('selected').length-1);
$counter.attr('value', sc.find('selected').length-1);
//update totalnum
$total.text(recalculateTotal(sc));
$total.attr('value', recalculateTotal(sc));
//Delete reservation
$('#cart-item-'+this.settings.id).remove();
//optional
return 'available';
} else if (this.status() == 'unavailable') { //sold
return 'unavailable';
} else {
return this.style();
}
}
});
The problem in particular is in part related to the recalculation of the total
$total.text(recalculateTotal(sc));
$total.attr('value', recalculateTotal(sc));
in practice this function calculates the total price for each click on a map and recalculates it when the item (deselecting) is again clicked. When I click to select an item, the function calculates the total correctly, when I click again an item to deselect it, in the first de-selection (or click), the amount is not subtracted.
To be more concrete give you an example: If I select three items for the price of 10 euro each, with each click the total is updated correctly (10 + 10 + 10 = 30); When I deselect the three elements, when i do the first click the total is not updated while in the following click (deselection) the total is updated so it does not return = 0 but 10.
So, I find no element selected, but the total takes me 10!
I tried to include this change but the calculation is completely wrong
...........
...........
$total.text(recalculateTotal(sc)+this.settings.data.price);
$total.attr('value', recalculateTotal(sc)+this.settings.data.price);
return 'selected';
}
alert('You can only choose '+ maxSeats + ' seats.');
return 'available';
} else if (this.status() == 'selected') { //Checked
......
......
//update totalnum
$total.text(recalculateTotal(sc)-this.settings.data.price);
$total.attr('value', recalculateTotal(sc)-this.settings.data.price);
any suggestion will be appreciated