2011-11-04 77 views
0

這裏是我的代碼:簡單的JavaScript空字符串檢查,(空字符串)是什麼意思,爲什麼它失敗?

$("#ddlCiudad").change(function() { 
    var idCity = $("#ddlCiudad").val(); 
    $.getJSON("/ProductCheckout/GetPriceForLocation", { cityId: idCity, productId: idProduct, type: "land" }, 
     function (cityData) { 

      console.log("Recieved json data.");      

      landCost = cityData.LandCost; 
      $("#billingshippingcost").text(landCost); 
      console.log("Assigned value of LandCost"); 

      airCost = cityData.AirCost; 
      console.log("Assigned value of AirCost"); 

      console.log(landCost); //Logs: 25,00 
      console.log(airCost); //Logs: "(an empty string)" 

      if (landCost == "") { 
       $(".land").removeClass("land").addClass("land-disabled"); 
      } 
      else { 
       $(".land-disabled").removeClass("land-disabled").addClass("land"); 
      } 

      if (airCost = "") { 
       $(".air").removeClass("air").addClass("air-disabled"); 
      } 
      else { 
       $(".air-disabled").removeClass("air-disabled").addClass("air"); 
      } 

     } 
    ); 
}); 

if聲明被解僱,爲什麼它不發射任何建議?可能an empty string與Javascript中的「」不一樣。

+7

是不是覺得應該是'如果(airCost == 「」)'? – NullUserException

+0

@NullUserExceptionఠ_ఠ是的,我已經修復了。 :)但錯誤仍然存​​在。這種情況沒有通過。 –

+1

你確定嗎?在if區塊中添加一些類似'console.log('airCost is empty')'的東西來檢查它是否到達那裏(並且它只是沒有被正確添加和移除的類) – NullUserException

回答

1

嘗試:

if (!airCost) { 
    $(".air").removeClass("air").addClass("air-disabled"); 
} 
else { 
    $(".air-disabled").removeClass("air-disabled").addClass("air"); 
} 
相關問題