$(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部分。並且控制檯顯示廉價值爲真,所以廉價值是真實的。這很奇怪!如果零件未執行
'if(cheap == true)'是多餘的(對眼睛不利);使用'if(cheap)' – royhowie