vendredi 7 juin 2019

Trying to multiply the values of several checkbox's but it returns a string

trying to multiply the numeric value of a checkbox but it is returning as string. my example is on JSFiddle https://jsfiddle.net/shiataz12/mjnqth3L/1/

The options represent a POST value and that will multiply the value of the mileage and other options according to that number.

I've tried running a check on document ready to run a function but it didnt quite work, I separated the functions into the two spans and i got a string returned.

Code available in JSfiddle link for ease of reference.

  $(document).ready(function(){
$('input[name="checkbox1"]').click(function(){
    $('input[name="checkbox2"]').prop('checked', false);
});
    $('input[name="checkbox2"]').click(function(){
    $('input[name="checkbox1"]').prop('checked', false);
});
    $('input[name="checkbox1"]').checked(function(){
    displayVals1();
    });

 });
var $cbs4 = $("#qr3");
function displayVals1() {
  Calculate1();
        Calculate2();
        var singleValues1 = $("#qr1").val();
        $cbs4.each(function() {
    if (this.checked)
        singleValues1 = parseInt(singleValues1) + parseInt(this.value);

    });
  $("#pricef1").text(singleValues1);
 }
var $cbs5 = $("#qr3");
function displayVals2() {
  Calculate3();
        Calculate4();
        var singleValues2 = $("#qr2").val();
        $cbs4.each(function() {
    if (this.checked)
        singleValues2 = parseInt(singleValues2) + parseInt(this.value);

    });
  $("#pricef1").text(singleValues2);
  }
 var $cbs = $("#qr3");
 function Calculate1() {
    var kms1 = $("#qr1").val();
var total = $("#mySelect").val();
$cbs.each(function() {
    if (this.checked)
        total = parseInt(total) * parseInt(this.value);
                kms1 =  parseInt(total) * parseInt(kms1);
                    });
$("#usertotal").text('R ' + total + kms1 +'/day');
}
var $cbs1 = $("#qr3");
function Calculate2() {
            var total1 = $("#qr1").val();
$cbs1.each(function() {
    if (this.checked)
        total1 = parseInt(total1) + parseInt(this.value);

    });
  $("#userdaily").text('R ' + total1 +'/day');
  }
  var $cbs2 = $("#qr3");
  function Calculate3() {
    var kms2 = $("#qr2").val();
  var total2 = $("#mySelect").val();
  $cbs2.each(function() {
    if (this.checked)
        total2 = parseInt(total2) * parseInt(this.value);
                kms2 =  parseInt(total2) * parseInt(kms2);

    });

  $("#usertotal").text('R ' + total2 + kms2 +'/day');
 }
 var $cbs3 = $("#qr3");
 function Calculate4() {
            var total3 = $("#qr2").val();
 $cbs3.each(function() {
    if (this.checked)
        total3 = parseInt(total3) + parseInt(this.value);

    });
$("#userdaily").text('R ' + total3 +'/day');
}
$("#qr1").change(displayVals1);
displayVals1();
$("#qr2").change(displayVals2);
displayVals2();
//For  checkboxes
// WHEN CHECKBOX CLICKED OR CHECKED - RUN CALCUSAGE //
$cbs.click(Calculate1);
$cbs1.click(Calclate2);




Aucun commentaire:

Enregistrer un commentaire