[英]Check If input contains value or another value
I am stuck on trivial issue, how to simply check if textarea contains "12" OR "34" value?我被困在一个小问题上,如何简单地检查 textarea 是否包含“12”或“34”值? Why does the code attached below does not work?
为什么下面附加的代码不起作用?
function check() { if (V1.value == ('12' || '34')) { alert('Yes'); } else { alert("No"); } }
<textarea id="V1"></textarea><br> <button onclick="check()">Check</button>
It should be if(V1.value == '12' || V1.value == '34')
.应该是
if(V1.value == '12' || V1.value == '34')
。
('12' || '34')
will simply evaluate to "12"
, so your statement is just checking if the value is 12. ('12' || '34')
将简单地评估为"12"
,因此您的语句只是检查值是否为 12。
function check() { if(V1.value == '12' || V1.value == '34') { alert('Yes'); } else { alert("No"); } }
<textarea id="V1"></textarea><br> <button onclick="check()">Check</button>
It should be V1.value === "12" || V1.value === "34"
应该是
V1.value === "12" || V1.value === "34"
V1.value === "12" || V1.value === "34"
. V1.value === "12" || V1.value === "34"
。
Also you need to get the #V1
input with:您还需要通过以下方式获取
#V1
输入:
const V1 = document.getElementById("V1");
const V1 = document.getElementById("V1"); function check() { if (V1.value === "12" || V1.value === "34") { alert("Yes"); } else { alert("No"); } }
<textarea id="V1"></textarea> <br /> <button onclick="check()">Check</button>
This can be done in a much simple way:这可以通过一种非常简单的方式完成:
const V1 = document.getElementById("V1"); function check() { if (V1.value === "12" || V1.value === "34") alert("Yes"); else alert("No"); }
<textarea id="V1"></textarea> <br /> <button onclick="check()">Check</button>
You can do this in this way also:你也可以这样做:
const V1 = document.getElementById("V1"); function check() { V1.value === "12" || V1.value === "34"? alert("Yes"): alert("No") };
<textarea id="V1"></textarea> <br /> <button onclick="check()">Check</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.