如何在javascript

时间:2016-08-03 18:53:58

标签: javascript logical-operators

我正在尝试使用if语句和or ||运算符来测试房间类型的选择框选项的值是单个King,单个Double Queen还是单个两个twins,然后执行一些代码。我一直收到消息"未被发现的语法错误未识别的令牌||"在哪里||运算符在if语句中。



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

hotel.name = "Bonton";

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


 function checkin(){
 	var nRooms = document.getElementById("numRooms").value * 1;
 	var tRoom = document.getElementById("typeRoom");
 	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//
 		
 		hotel.booked = numRooms + hotel.booked;
 	
 		if hotel.checkAvailability() < 0 {
 			var rAvail = nRooms + (hotel.checkAvailability());
 			if rAvail <= 0 {
 				var noSay = document.getElementById("say");
 				noSay.textContent = "We have no rooms available";
            }
          
             else {
             	var yesSay = document.getElementById("say");
            	yesSay.textContent = "We have" + rAvail + "rooms available";
         
 		       }

 		    }
&#13;
<body>
	   <div id="wrapper">
		<div id="heading">
			<h1 id="hotelN"></h1>
        /div>
		<div id="main">
			<form id="myForm">
			<fieldset>
			<legend>Book a Room</legend><p>
			<label for="rm1">Number of Rooms:</label>	
            <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>
			</form>
			<H1 class="al">Hotel Room Availability</H1>
		    <p class="al" ><span id="say"></span> and check out on July <span  id="dateOut"></span></p>
			
            
		
		<p id="first">jjjj</p>
        
		<p id="second"></p>

		</div>	
</div>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:4)

您只需要添加一对括号:

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

或者

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

答案 1 :(得分:0)

if语句的整个条件部分必须用括号括起来。通过执行if (condition1) || (condition2),您可以在||之前终止条件。

它会是这样的: if ((condition1) || (condition2))

此外:您正在寻找的条件运算符是==,而不是===用于比较两个值,而=用于设置

答案 2 :(得分:0)

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

您无法在if()

之前关闭||
if (tRoom.value = "singleK" || tRoom.value ="singleQ" || tRoom.value = "singleT") {

答案 3 :(得分:0)

该行至少有2个问题:

  1. 您的主要问题是您在整个条件下错过了一组括号:

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

  2. 现在你的or-operator只是浮动的。

    1. 您需要使用===进行比较。 =用于分配。