2

如果选中复选框,我试图将计算乘以 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/

4

1 回答 1

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);
});
于 2013-05-10T20:28:26.023 回答