2015-04-23 51 views
1
$(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部分。並且控制檯顯示廉價值爲真,所以廉價值是真實的。這很奇怪!如果零件未執行

+0

'if(cheap == true)'是多餘的(對眼睛不利);使用'if(cheap)' – royhowie

回答

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

}); 
0

嘗試改變,

if(cheap==true){ 

if(cheap === true){ 

對於解釋,看看this答案:

==運營商將做任何必要的類型轉換後相等比較。 ===運營商不會做轉換,所以如果兩個值不是相同的類型===將簡單地返回false。在這種情況下,===會更快,並且可能會返回與==不同的結果。在所有其他情況下,性能將是相同的。

+1

請解釋有什麼區別 –

+0

它是相同的字符串相同類型比較.i.e相等值和相等類型比較。那麼這可能對你的情況沒有幫助,但這是值得一試的。 –

+1

@ByteHamster版很棒.. –