如何在javascript中使用Or运算符
How to use Or operator in javascript
我正在尝试使用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") {
这行至少有两个问题:
你的主要问题是,你在整个条件周围缺少了一组括号:
if ((tRoom.value = "singleK") || ...) {
现在您的或运算符只是浮动的。
- 您需要使用
===
进行比较。=
用于分配
相关文章:
- AND 和 OR 运算符、正则表达式和值长度
- OR 运算符,用于转义 JavaScript 中的未定义
- javascript函数之间的OR运算符
- 由 OR 运算符分隔的语句的计算顺序
- JavaScript 正则表达式 OR 运算符
- 如果存在值,则使用值,否则使用 or 运算符分配默认值
- 在 JavaScript 中,条件语句中 OR 运算符的正确语法是什么?我错过了什么
- 通过使用 AngularJS 遵循 OR 运算符仅过滤对象中的特定字段
- jQuery OR 运算符混淆
- 多个 OR 运算符与 elem.value.match
- 为什么有些值通过 OR 运算符,而其他值则不传递
- Javascript 如何使用带括号的 and/or 运算符
- OR 运算符多项选择脚本
- 我应该使用 if-then-else 或 and or 运算符来检查 Javascript 中的空值吗?
- 实数的OR运算符
- 组合2个正则表达式,其中一个使用OR运算符进行精确匹配
- Javascript中的OR运算符(|)的作用与parseInt相同
- 在javascript switch语句中使用OR运算符
- 如何在javascript中使用Or运算符
- If语句中的OR运算符