Question

I'm trying to sum the value of an input text changing dynamically with a radio that also changes dynamically. I'm doing something right but also something wrong because it doesn't sum when I want. The sum should show everytime the input text changes and not randomly disappear then you click a radio, just sum them.

the fiddle http://jsfiddle.net/7tFhx/

and the code

<form id="myForm" accept-charset="UTF-8">
    <button type="button" class="btn-tt btn-primary btn-lg" disabled>1. Elige el color</button></br>
        <label>
            <input class="calc" id="fb1" type="radio" name="fb" value="10">
            <img src="img/01.jpg"/>
        </label>            
        <label>
            <input class="calc" id="fb2" type="radio" name="fb" value="15">
            <img src="img/02.jpg"/>
        </label>
    </br>
    </br>
    <button type="button" class="btn-tt btn-primary btn-lg" disabled>2. Elige las medidas</button></br>
    <div class="row">
        <div class="col-xs-2">
            ancho (cm.)
            <input id="ancho" type="text" class="form-control" placeholder="100">
        </div>
        <div class="col-xs-2">
            alto (cm.)
            <input id="alto" type="text" class="form-control" placeholder="100">
        </div>      
        <div class="col-xs-2">
            cantidad
            <input id="cantidad" type="text" class="form-control" placeholder="1">
        </div>
    </div>
    </br>
    <button type="button" class="btn-tt btn-primary btn-lg" disabled>3. Posición mecanismo</button></br>
        <label>
            <input class="calc" id="fb3" type="radio" name="fb1" value="20">
            <img src="img/01.jpg"/>
        </label>            
        <label>
            <input class="calc" id="fb4" type="radio" name="fb1" value="35">
            <img src="img/02.jpg"/>
        </label>

    <div>
        Total: <span id="price">0</span>€
    </div>
</form>

js:

$(function(){    

var mecanismoMedida = ['100','200'];
var mecanismoPrecio = ['50','80'];

$('#ancho').on('input',function(){
var j = 1, i = 0;
value = parseInt(($("#ancho").val() / 8) + 1);

for(i = 0; i < mecanismoMedida.length; i++){
    if($("#ancho").val() >= mecanismoMedida[i] && $("#ancho").val() < 
mecanismoMedida[j]){
        value += mecanismoPrecio[i];
        break;
    } else {
        j++;
    }
}

$("#price").text(this.value + value);
});

$('#myForm input[type="radio"]').on('change', function () {
    var sum = 0;
    $("#myForm").find("input[type='radio']:checked").each(function () {
        sum += parseInt(this.value);
    });
    $("#price").text(sum);
});
});
Was it helpful?

Solution

If it's a simple sum you're after, you can simply run an update code on the keyup and change events of all input elements:

$("input").change(function(){update();}).keyup(function(){update();});

To update them, first you need to check and see which one of the two radio buttons are checked and get its value:

function update(){

    var p = 0;
    for(var i = 0; i < $(".calc").val(); i++)
    {
        if($($(".calc")[i]).prop("checked") == true)
        {
            p += parseInt($($(".calc")[i]).val());
        }
    }

After that, simply parse the values of the textboxes to integers and add them to the value of the radio button. I created a custom function to do this, because if the boxes are empty, parsing them returns NaN:

    p = parseInt(p);

    p += parseInt(val2($("#ancho")));
    p += parseInt(val2($("#alto")));
    p += parseInt(val2($("#cantidad")));

    p = parseInt(p);

    $("#price").html(p);
}

function val2(elm){
    if(isNaN(parseInt($(elm).val())))
       return 0;
    else
        return parseInt($(elm).val());
}

JSFiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top