0

i try to return some amount when user click checkbox and do some calculation. But the result always return wrong value if i try to check two checkbox.

$('.type_checkbox input[type="checkbox"]').each(function (index) {

    if ($(this).is(':checked')) {
            checkbox_price_prefix = $(this).data('prefix-checkbox');
            checkbox_price = checkbox_price_prefix + $(this).data('price-checkbox');   // <----- += or =
    }
});

You can try HERE

The problem is when user try to check type A and type B, the result suppose to be RM90

2
  • 2
    Not related with question: var optionCheckbox = function optionCheckbox() { is awesome. Commented Mar 5, 2015 at 9:48
  • And fiddle from me with rewritten code. Commented Mar 5, 2015 at 10:02

4 Answers 4

2

You are not totaling you count - you are replacing it. change:

checkbox_price = checkbox_price_prefix + $(this).data('price-checkbox');

To:

checkbox_price += parseFloat(checkbox_price_prefix + $(this).data('price-checkbox'));
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that you are overwritting the variables' value in each loop iteration...

var checkbox_price = 0;
$('.type_checkbox input[type="checkbox"]').each(function (index) {
    if ($(this).is(':checked')) {
        checkbox_price += parseInt($(this).data('prefix-checkbox') + $(this).data('price-checkbox'),10);
    }
});

Declaring the variable outside the scope of each, and then increment its value with the values of the checked inbox will do as you want.

And with some jQuery selector optimisation:

var checkbox_price = 0;
$('.type_checkbox input[type="checkbox"]:checked').each(function (index) {
        checkbox_price += parseInt($(this).data('prefix-checkbox') + $(this).data('price-checkbox'),10);
});

Comments

0

There are a number of problems here:

  • You are overwriting the total value instead of summing them together
  • You cannot get the value -20 by concatenating the strings - and 20.

You also don't need to check :checked on each loop. Just include that in your selector:

var optionCheckbox = function optionCheckbox() {
    var totalAdjustment = 0;

    $('.type_checkbox input[type="checkbox"]:checked').each(function () {
        var prefix = $(this).data('prefix-checkbox');
        var price = $(this).data('price-checkbox');
        totalAdjustment += prefix === '-' ? -price : +price;
    });

    return totalAdjustment ;
};

1 Comment

You cannot get the value -20 by concatenating the strings "-" and "20". What about parseFloat("-" + "20");, which does exist in original code?
0

Your problem is in your calculate function

 total = 100 + parseFloat(option_checkbox_return);

100 must be declared as global variable and initialised to 100

var x=100;
var optionCheckbox = function optionCheckbox() {

    var checkbox_price_prefix;
    var checkbox_price = 0;
    // Loop Checkbox
    $('.type_checkbox input[type="checkbox"]').each(function (index) {

        if ($(this).is(':checked')) {
            checkbox_price_prefix = $(this).data('prefix-checkbox');
            checkbox_price = checkbox_price_prefix + $(this).data('price-checkbox');
        }
    });

    return checkbox_price;
}

    function calculateTotal() {

        var option_checkbox_return = optionCheckbox();

        total = x + parseFloat(option_checkbox_return);
        x=total;
        result = parseFloat(Math.round(total * 100) / 100).toFixed(2); // Convert to 2 decimal places
        $('.price_placeholder').html('RM' + result);

    }

$('.type_checkbox input[type="checkbox"]').on('click', function () {
    calculateTotal();
});

2 Comments

@Regent i edited my answer and it will works :). i added the js code
Even though you didn't mention x = total; (which is important part of your idea), code is still incorrect. Here is fiddle, showing that it works incorrectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.