2016-08-03 38 views
-3

我正在嘗試使用if語句,運算符來測試房間類型的選擇框選項的值是單個King,單個Double Queen還是單個雙胞胎,然後執行一些代碼。我不斷收到消息「uncaught語法錯誤未識別的令牌||」正確的地方||運算符在if語句中。如何在javascript中使用或運算符

var hotel = { 
 
\t name:"Benson hotel", 
 
\t rooms:500, 
 
\t jsuites:10, 
 
\t ssuites:10, 
 
\t booked:100, 
 
\t checkAvailability: function(){ 
 
\t return this.rooms - this.booked; 
 
} 
 
     
 
}; 
 

 
hotel.name = "Bonton"; 
 

 
var elName = document.getElementById("hotelN"); 
 
elName.textContent = hotel.name; 
 

 

 
function checkin(){ 
 
    \t var nRooms = document.getElementById("numRooms").value * 1; 
 
    \t var tRoom = document.getElementById("typeRoom"); 
 
    \t if (tRoom.value = "singleK")||(tRoom.value ="singleQ")||(tRoom.value = "singleT") { //*I am getting the following message about above line of code in the console. "uncaught syntax error unidentified token ||" Obviously the above if statement with || operator is not correct// 
 
    \t \t 
 
    \t \t hotel.booked = numRooms + hotel.booked; 
 
    \t 
 
    \t \t if hotel.checkAvailability() < 0 { 
 
    \t \t \t var rAvail = nRooms + (hotel.checkAvailability()); 
 
    \t \t \t if rAvail <= 0 { 
 
    \t \t \t \t var noSay = document.getElementById("say"); 
 
    \t \t \t \t noSay.textContent = "We have no rooms available"; 
 
      } 
 
      
 
      else { 
 
       \t var yesSay = document.getElementById("say"); 
 
      \t yesSay.textContent = "We have" + rAvail + "rooms available"; 
 
      
 
    \t \t  } 
 

 
    \t \t  }
<body> 
 
\t <div id="wrapper"> 
 
\t \t <div id="heading"> 
 
\t \t \t <h1 id="hotelN"></h1> 
 
     /div> 
 
\t \t <div id="main"> 
 
\t \t \t <form id="myForm"> 
 
\t \t \t <fieldset> 
 
\t \t \t <legend>Book a Room</legend><p> 
 
\t \t \t <label for="rm1">Number of Rooms:</label> \t 
 
      <input type="number" max="10" min="1" value="1"name="rm" id="numRooms"> 
 
      Type of room: <select type="text" maxlength="14" id="typeRoom"> 
 
    <option value="singleK">Single King Bed</option> 
 
    <option value="singleQ">Single Queen Bed</option> 
 
    <option value="singleT">Single Two Twins</option> 
 
    <option value="jsuite">Junior One Bedroom Suite</option> 
 
    <option value="srsuite">Senior Two Bedroom Suite</option> 
 
</select></p> 
 
      Occupancy:<select type="text"> 
 
      <option>One adult</option> 
 
      <option>Two adults</option> 
 
      <option>One adult and Child</option> 
 

 
      </select> 
 
      
 
      Check In Date: <input type="date"name="checkin" id="cInDate"> 
 
      Check Out Date: <input type="date" name="checkout" id="cOutDate">   
 
      <button type="button" onclick="checkin()">Submit</button> 
 
</fieldset> 
 
\t \t \t </form> 
 
\t \t \t <H1 class="al">Hotel Room Availability</H1> 
 
\t \t  <p class="al" ><span id="say"></span> and check out on July <span id="dateOut"></span></p> 
 
\t \t \t 
 
      
 
\t \t 
 
\t \t <p id="first">jjjj</p> 
 
     
 
\t \t <p id="second"></p> 
 

 
\t \t </div> \t 
 
</div>

+2

'if hotel.checkAvailability()<0' - 在您的實際代碼中還是粘貼錯誤?所有的條件都需要包裹在parens中。 – tymeJV

+0

'if rAvail <= 0'也有錯誤。需要括號:'if(rAvail <= 0)' – 4castle

回答

4

你只需要添加一對括號:

if ((tRoom.value == "singleK")||(tRoom.value =="singleQ")||(tRoom.value == "singleT")){ 
... 
} 

或者

if (tRoom.value == "singleK"||tRoom.value =="singleQ"||tRoom.value == "singleT"){ 
... 
} 
+1

並將'='改爲'===' – 4castle

+0

謝謝!編輯完成。 –

+1

您不需要將每個條件都包含在一組括號中。你可以把整個東西包裝在一個集合中。 – zfrisch

0

if語句的所有條件部分必須被包裹在括號。通過執行if (condition1) || (condition2),您可以終止||之前的條件。

這會是這樣,而不是: if ((condition1) || (condition2))

此外:你要找的條件運算符是==,不===用於比較兩個值,而=用於集合

0
if (tRoom.value = "singleK")||(tRoom.value ="singleQ")||(tRoom.value = "singleT") { 

你不能接近if()||

​​
0

你至少有2個問題與該行:

  1. 你的主要問題是,你缺少一套支架圍繞整個條件:

    if ((tRoom.value = "singleK") || ...) {

現在你的or-運算符只是浮動的。

  1. 您需要使用===進行比較。 =用於分配。