Javascript:如何在 if 构造中获得一个变量可以等于的可能值列表
Javascript: how to have a list of possible values a a variable can equal in an if construct
我希望我的 textarea 的值是 1、2 和 3 的列表,具体取决于提示中提供的数字。但是,它仅适用于每个列表的第一个数字。
list = "";
onclick = function() {
room = prompt("room");
if (+room == (108 || 110 || 112 || 401 || 403 || 405 || 600 || 601 || 602 || 603 || 604 || 605 || 606 || 607 || 608 || 611 || 613 || 615 || 617 || 619 || 621)) {
list += "1";
document.getElementById("p").value = list;
}
if (+room == (205 || 300 || 302 || 304 || 400 || 102 || 408 || 409 || 410 || 411 || 412 || 413 || 614 || 616 || 618 || 625 || 627 || 629)) {
list += "2";
document.getElementById("p").value = list;
}
if (+room == (301 || 305 || 307 || 310 || 311 || 312 || 313 || 314 || 315 || 316 || 317 || 320 || 321 || 323 || 324 || 325 || 326 || 327 || 331 || 404 || 501 || 512)) {
list += "3";
document.getElementById("p").value = list;
}
};
<textarea id="p" /></textarea>
您可以将值放在一个数组中,然后在此处使用 .indexOf()
,例如:
if([108,110,112,401,403,405,600,601,602,603,604,605,606,607,608,611,613,615,617,619,621].indexOf(parseInt(room)) >= 0){
list+="1";
document.getElementById("p").value=list;
}
我已经更新了您的代码,创建了一个数组并使用了 IndexOf 属性,
请参考以下,
<html>
<head>
<title>
</title>
</head>
<body>
<textarea id="p" /></textarea>
<script type="text/javascript">
list="";
onclick=function(){
room=prompt("room");
var array = [108,110,112,401,403,405,600,601,602,603,604,605,606,607,608,611,613,615,617,619,621];
if(array.indexOf(parseInt(room)) !== -1)
{
list+="1";
document.getElementById("p").value=list;
}
var array_1 = [205,300,302,304,400,102,408,409,410,411,412,413,614,616,618,625,627,629];
if(array_1.indexOf(parseInt(room)) !== -1)
{
list+="2";
document.getElementById("p").value=list;
}
var array_2 = [301,305,307,310,311,312,313,314,315,316,317,320,321,323,324,325,326,327,331,404,501,512];
if(array_2.indexOf(parseInt(room)) !== -1)
{
list+="3";
document.getElementById("p").value=list;
}
};
</script>
</body>
</html>
希望这能解决,
谢谢
此致
我希望我的 textarea 的值是 1、2 和 3 的列表,具体取决于提示中提供的数字。但是,它仅适用于每个列表的第一个数字。
list = "";
onclick = function() {
room = prompt("room");
if (+room == (108 || 110 || 112 || 401 || 403 || 405 || 600 || 601 || 602 || 603 || 604 || 605 || 606 || 607 || 608 || 611 || 613 || 615 || 617 || 619 || 621)) {
list += "1";
document.getElementById("p").value = list;
}
if (+room == (205 || 300 || 302 || 304 || 400 || 102 || 408 || 409 || 410 || 411 || 412 || 413 || 614 || 616 || 618 || 625 || 627 || 629)) {
list += "2";
document.getElementById("p").value = list;
}
if (+room == (301 || 305 || 307 || 310 || 311 || 312 || 313 || 314 || 315 || 316 || 317 || 320 || 321 || 323 || 324 || 325 || 326 || 327 || 331 || 404 || 501 || 512)) {
list += "3";
document.getElementById("p").value = list;
}
};
<textarea id="p" /></textarea>
您可以将值放在一个数组中,然后在此处使用 .indexOf()
,例如:
if([108,110,112,401,403,405,600,601,602,603,604,605,606,607,608,611,613,615,617,619,621].indexOf(parseInt(room)) >= 0){
list+="1";
document.getElementById("p").value=list;
}
我已经更新了您的代码,创建了一个数组并使用了 IndexOf 属性, 请参考以下,
<html>
<head>
<title>
</title>
</head>
<body>
<textarea id="p" /></textarea>
<script type="text/javascript">
list="";
onclick=function(){
room=prompt("room");
var array = [108,110,112,401,403,405,600,601,602,603,604,605,606,607,608,611,613,615,617,619,621];
if(array.indexOf(parseInt(room)) !== -1)
{
list+="1";
document.getElementById("p").value=list;
}
var array_1 = [205,300,302,304,400,102,408,409,410,411,412,413,614,616,618,625,627,629];
if(array_1.indexOf(parseInt(room)) !== -1)
{
list+="2";
document.getElementById("p").value=list;
}
var array_2 = [301,305,307,310,311,312,313,314,315,316,317,320,321,323,324,325,326,327,331,404,501,512];
if(array_2.indexOf(parseInt(room)) !== -1)
{
list+="3";
document.getElementById("p").value=list;
}
};
</script>
</body>
</html>
希望这能解决,
谢谢
此致