javascript不等于字符串检查返回true

时间:2015-12-16 19:06:36

标签: javascript string

我有一个简单的java脚本函数可以检查行程类型是否安全往返

    function CheckIfSafeRideRoundTrip()
    {
        var comboBox = $find("<%=rcmb_TripType.ClientID%>");
        var selectedTripType = comboBox.get_selectedItem().get_text();
        var isAirportRoundTrip = false;
        if(selectedTripType == "Safe Ride - Round Trip");
            isAirportRoundTrip = true;
        return isAirportRoundTrip;

    }

selectedTripType值返回&#34; Round Trip - Airport&#34;但是对于某些为什么比较布尔表达式返回true并将isAirportRoundTrip设置为true rcmb_TripType是一个telerik radComboBox,它与我通过调试器确保selectedTripType值无关。 作为附加信息我在这里调用函数

    function BuildConfirmationLayout()
    {
        var divOthersAddressInfo = document.getElementById("div_OthersLocationInfo");
        var divAirportAddressInfo = document.getElementById("div_AirPortLocationInfo");
        var divAirportRoundTrip = document.getElementById("div_Airport_SecondTicketDate");
        var divSaferideRoundTrip = document.getElementById("div_SafeRide_Standby");
        var isAirport = CheckIfAirportTrip();
        var isAirportRoundTrip = CheckIfAirportRoundTrip();
        var isSafeRideRoundTrip = CheckIfSafeRideRoundTrip();

        if (isAirport) {
            divOthersAddressInfo.style.display = "none";
            divAirportAddressInfo.style.display = "block";
            divAirportRoundTrip.style.display = "none";
            divSaferideRoundTrip.style.display = "none";
        }
        else {
            divAirportAddressInfo.style.display = "none";
            divOthersAddressInfo.style.display = "block";
            divAirportRoundTrip.style.display = "none";
            divSaferideRoundTrip.style.display = "none";
        }

        if(isAirportRoundTrip)
        {
            divAirportRoundTrip.style.display = "none";
            divSaferideRoundTrip.style.display = "block";
        }

        if(isSafeRideRoundTrip)
        {
            divAirportRoundTrip.style.display = "none";
            divSaferideRoundTrip.style.display = "block";

        }

    }

3 个答案:

答案 0 :(得分:3)

这是因为你在这一行之后有分号:

if(selectedTripType == "Safe Ride - Round Trip");

结束if语句,并始终执行下一行。

答案 1 :(得分:2)

if语句后的半冒号终止该行,删除它,你应该没事。

if(selectedTripType == "Safe Ride - Round Trip"); <-- remove this semi colon

答案 2 :(得分:1)

if(selectedTripType == "Safe Ride - Round Trip");

这里有;。这与:

相同
if(selectedTripType == "Safe Ride - Round Trip"){}

你打算做的是:

if(selectedTripType == "Safe Ride - Round Trip"){
    isAirportRoundTrip = true;
}

(您只需要丢失;。为了清楚起见,我只添加了{}; 始终拥有它们是个好主意。)