如果选中则更改选择值不起作用

时间:2013-05-10 20:22:52

标签: jquery checkbox

如果选中复选框,我试图将计算乘以2,但不起作用

$('#kilometros').keyup(function () {

    var kilometros = parseFloat($(this).val().toString().replace(",", "."));
    var precioKilometros = parseFloat($('#precioKilometros').val());
    if ($('#retorno').is(':checked')) {
        var retorno = 2.0;
    } else {  
        var retorno = 1.0;
    }

    var totalKilometros = (kilometros * precioKilometros * retorno).toFixed(2).toString().replace(".", ",");
    $('#totalKilometros').html(totalKilometros);
});

http://jsfiddle.net/3k9sz/1/

1 个答案:

答案 0 :(得分:0)

试试这个:

var retorno = $('#retorno').is(':checked') ? 2.0 : 1.0;

而不是:

if ($('#retorno').is(':checked')) {
    var retorno = 2.0;
} else {  
    var retorno = 1.0;
}

所以,你的代码应该是:

$('#kilometros').keyup(function () {
    var kilometros = parseFloat($(this).val().toString().replace(",", "."));
    var precioKilometros = parseFloat($('#precioKilometros').val());
    var retorno = $('#retorno').is(':checked') ? 2.0 : 1.0;
    var totalKilometros = (kilometros * precioKilometros * retorno).toFixed(2).toString().replace(".", ",");
    $('#totalKilometros').html(totalKilometros);
});