如何在javascript中使用Or运算符

How to use Or operator in javascript

本文关键字:Or 运算符 javascript      更新时间:2023-09-26

我正在尝试使用if语句和or ||运算符来测试房间类型的选择框选项的值是single King、single Double Queen还是single two 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";
         
 		       }
 		    }
<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>

您只需要添加一对括号:

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

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

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

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

此外:您要查找的条件运算符是==,而不是===用于比较两个值,而=用于<em]设置>。

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

您不能在|| 之前关闭if()

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

这行至少有两个问题:

  1. 你的主要问题是,你在整个条件周围缺少了一组括号:

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

现在您的或运算符只是浮动的。

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