如果部分没有执行

时间:2015-04-23 06:56:41

标签: javascript jquery

$(document).ready(function () {
    var t=true;
    var f=false;
    var cheap;
    $('.day1').on('change', function (e) {
        if($(this).val() == "Saturday"){
            cheap = true;
        }
        else{
            cheap=false;
        }
    });
    if(cheap==true){
        $('.pricing1').change(function () {
        var price = parseFloat($('.total').data('base-price')) || 0;
        $('.pricing1').each(function (i, el) {
            price += parseFloat($('option:selected', el).data('cheap'));
            $('.total').val('$' + price.toFixed(2));
        });
        //console.log('cheap',cheap)
        });
    }
    else{
        $('.pricing').change(function () {
        var price = parseFloat($('.total').data('base-price')) || 0;
        $('.pricing').each(function (i, el) {
            price += parseFloat($('option:selected', el).data('price'));
            $('.total').val('$' + price.toFixed(2));
        });
        console.log('cheap',cheap)
        });
    }

});

当选择星期六时,控制台读数返回true以便宜。但if部分未执行。每次只有部分执行。逻辑上它应该执行if部分,如果廉价是真的。并且控制台将廉价值显示为true,因此cheap的值为true。这很奇怪!

2 个答案:

答案 0 :(得分:2)

您正在dom准备好注册事件处理程序,那时cheap的值为false,因此if条件不会得到满足,因此只有else部分中的更改处理程序将注册。

$(document).ready(function () {
    var t = true;
    var f = false;
    var cheap;
    $('.day1').on('change', function (e) {
        if ($(this).val() == "Saturday") {
            cheap = true;
        } else {
            cheap = false;
        }
    });
    $('.pricing1').change(function () {
        if (cheap == true) {
            var price = parseFloat($('.total').data('base-price')) || 0;
            $('.pricing1').each(function (i, el) {
                price += parseFloat($('option:selected', el).data('cheap'));
                $('.total').val('$' + price.toFixed(2));
            });
            //console.log('cheap',cheap)
        } else {
            var price = parseFloat($('.total').data('base-price')) || 0;
            $('.pricing').each(function (i, el) {
                price += parseFloat($('option:selected', el).data('price'));
                $('.total').val('$' + price.toFixed(2));
            });
            console.log('cheap', cheap)
        }
    });

});

您可以将代码简化为

$(document).ready(function () {
    var t = true;
    var f = false;
    var cheap;
    $('.day1').on('change', function (e) {
        if ($(this).val() == "Saturday") {
            cheap = true;
        } else {
            cheap = false;
        }
    });
    $('.pricing1').change(function () {
        var data = cheap ? 'cheap' : 'price';
        var price = parseFloat($('.total').data('base-price')) || 0;
        $('.pricing1').each(function (i, el) {
            price += parseFloat($('option:selected', el).data(data)) || 0;
        });
        $('.total').val('$' + price.toFixed(2));
    });

});

答案 1 :(得分:0)

尝试更改,

 if(cheap==true){

 if(cheap === true){

有关解释,请查看this回答:

  

==运算符将在执行任何必要的类型转换后比较相等性。 ===运算符不会执行转换,因此如果两个值不相同,则===将返回false。在这种情况下,===会更快,并且可能会返回与==不同的结果。在所有其他情况下,表现将是相同的。