Nielsen Rechia
Nielsen Rechia

Reputation: 649

Jquery How to sum in a radio button

I'm trying sum many values from radio buttons, but I'm with some problems in my logical thinking,

I put my source code here to a better visualization.

so, for exemplo I have the product_price = 9.00 and a radio = 2.00 and another radio, radio2= 0.00

If I check the radio, the sum will be sum = product_price + radio, but the problem is when I change the radio for radio2, it isn't work fine.

thanks

Upvotes: 0

Views: 811

Answers (3)

Guilherme Franco
Guilherme Franco

Reputation: 26

To be honest. I think this is the best idea.

http://jsfiddle.net/zEaw6/23/

Don't get the value html a user text as SuperSkunk said. BUT take from HTML. Create a attribute to your html and take the value that will be add from that.

Code is here:

var price =9.95; //Valor base
$('.rule input[type=radio]').change(function() {
var checkeds = $(".rule").find("input[type=radio]:checked");
var plus = 0;
for(var i =0; i<checkeds.length;i++)
{
    plus += parseFloat($(checkeds[i]).attr("data-preco"));
}
//arrumando as casas decimais
var rounded = Math.round((plus+price)*100)/100;
return $('.item_price_total').html(rounded);
});​

"preco" is price "Valor base" is standard value "arrumando as casa decimais" is arranging decimal places

Upvotes: 0

njord
njord

Reputation: 1288

I think it's better to not use HTML values to calcul stuff. You should generate some data from your server and, for example:

<script>
var cart = {
    currency: '$',
    currentTotal: 9.85
}
var products = {
    10: {price: 4.5},
    11: {price: 5.8}
};
</script>

Like that you'll be able to find the price easily with your item id.

Then, it's more logical, and in my opinion prettier to use the property "for" of labels:

<input id="choise_items_2__10" name="choise_items[2][]" type="radio" value="10">
<label class="radio" for="choise_items_2__10">Sanduíche 15 cm</label>

Finally, you should use a global price, it's more logical and it'll be better when you'll look back at your code.

Same as the way you get your jQuery element, don't hesitate to add step:

var $radioItem = $(this),
    $item = $radioItem.parent(),
    // we use the context argument 
    // same as $item.find('price')
    $priceItem = $('.price', $item);

You can find more details there: http://jsfiddle.net/zEaw6/19/

Upvotes: 0

Guilherme Franco
Guilherme Franco

Reputation: 26

I don't know if I understand it right....

But I guess that are you trying sum just if that option have some price...

If is this, you need be careful with the value that you are adding.

First, make a global variable called price. And set it to zero. That will be a price that you will add IF it is necessary. The default configuration, this value will be zero... so set it to zero.

When you change a option, first remove the old plus value from our total. Then you check if this option has some value to plus.... if it has, you have to update your global variable, and add this new value to total.

Look up the code for more details!

http://jsfiddle.net/zEaw6/17/

var price =0;
$('.rule input[type=radio]').change(function() {
var sum, total_current, currentPrice;
currentPrice = $(this).parent().parent().find('.price').text().replace('R$ ', '').replace('.', ',').replace(',', '.');
total_current = $('.item_price_total').text().replace('Total: R$ ', '').replace('.', ',').replace(',', '.');
sum = parseFloat(total_current) - parseFloat(price);
if (currentPrice) {
    price = currentPrice;
}
else{
    price = 0;
}
sum += parseFloat(price);
return $('.item_price_total').html(sum);

});​

Kind regards!

Upvotes: 1

Related Questions