Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果部分未执行_Javascript_Jquery - Fatal编程技术网

Javascript 如果部分未执行

Javascript 如果部分未执行,javascript,jquery,Javascript,Jquery,选择“星期六”时,控制台读数返回true表示便宜。但是if部分没有执行。每次只执行其他部分。逻辑上,如果cheap为true,它应该执行if部分。控制台将廉价值显示为true,因此廉价值为true。这太奇怪了 您正在dom就绪状态下注册事件处理程序,此时cheap的值为false,因此if条件不会得到满足,因此只有else部分中的更改处理程序才会得到注册 $(document).ready(function () { var t=true; var f=false; va

选择“星期六”时,控制台读数返回true表示便宜。但是if部分没有执行。每次只执行其他部分。逻辑上,如果cheap为true,它应该执行if部分。控制台将廉价值显示为true,因此廉价值为true。这太奇怪了

您正在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;
        }
    });
    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)
        });
    }

});

您可以将代码简化为

$(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));
    });

});

有关解释,请查看答案:

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


如果(便宜==true)
是多余的(而且令人不快);使用
如果(便宜)
请解释它的同一字符串同一类型比较有什么区别。即等值和等类型比较。这对你来说可能没什么帮助,但值得一试。
 if(cheap==true){
 if(cheap === true){