Javascript document.getElementById('textbox').value 停止工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14062782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:37:04  来源:igfitidea点击:

document.getElementById('textbox').value stopped working

javascripthtmlgetelementbyid

提问by user1743825

my code is very simple. I want the user to be able to type in a restaurant name and have my function tell them whether that restaurant serves Coke or Pepsi products. It worked fine an hour ago, but stopped working. Now, it shows the work "Pepsi" no matter what you type in. My guess is that there is something wrong with the if statements.

我的代码很简单。我希望用户能够输入餐厅名称并让我的函数告诉他们该餐厅供应可乐或百事可乐产品。一个小时前它工作正常,但停止工作。现在,无论您输入什么,它都会显示作品“Pepsi”。我的猜测是 if 语句有问题。

<html>

<noscript><b><center><font size="16">This app requires JavaScript to be enabled. Enable JavaScript on your browswer to make it work.</font></center></b></noscript>

<center>
<form onsubmit="popproducts()">

<br><br><br><br><br><br><br><br><br>
<input id="textbox" type="text" style="height: 100px; width: 500px" value=""></input>
</br></br></br></br></br></br></br></br></br>

</form>
</center>

<script type="text/javascript">

function popproducts()
{
 if(document.getElementById('textbox').value == "Cougars" || "Kane County Cougars" || "Cougars Baseball")
 {
  document.write('<center><br><font color="#0000FF" size=20></b>Pepsi</b></font></br></center>');
  document.write('<br><br><center><form><input type="button" style="height: 100px; width: 100px" value="Back" onClick="history.go(-1);return true;"></form></center></br></br>');
 }
 if(document.getElementById('textbox').value == "Burge" || "Iowa" || "University of Iowa")
 {
  document.write('<center><br><font color="#FF0000" size=20><b>Coke</b></font></br></center>');
  document.write('<br><br><center><form><input type="button" style="height: 100px; width: 100px" value="Back" onClick="history.go(-1);return true;"></form></center></br></br>');
 }
}

</script>

</html>

回答by MadSkunk

It's the line:

这是线:

if(document.getElementById('textbox').value == "Cougars" || "Kane County Cougars" || "Cougars Baseball")

It shouldbe:

应该是:

if(document.getElementById('textbox').value == "Cougars" || document.getElementById('textbox').value == "Kane County Cougars" || document.getElementById('textbox').value == "Cougars Baseball")

The first part evaluates as trueor falsedepending on what you type, but the second and third will always evaluate as true.

第一部分评估为truefalse取决于您键入的内容,但第二部分和第三部分将始终评估为true

回答by JCOC611

This is your issue:

这是你的问题:

if(document.getElementById('textbox').value == "Cougars" || "Kane County Cougars" || "Cougars Baseball")

When you have an ifstatement with the oroperator ||, the praser interprets it as if it was:

当您有一个if带有or运算符的语句时||,praser 会将其解释为:

if(doc....value == "Cougars || "Kane Country Cougars" != false...)

In other words, the if statement is always truebecause the string "Kane Country Cougars"isn't false. To fix it, you should do this:

换句话说,if 语句始终为真,因为字符串"Kane Country Cougars"不是假的。要修复它,你应该这样做:

var val = document.getElementById('textbox').value;
if(val == "Cougars" || val == "Kane County Cougars" || val == "Cougars Baseball")

回答by elclanrs

All other answers are fine but here's how I usually do it:

所有其他答案都很好,但这是我通常的做法:

var value = document.getElementById('textbox').value;

if ( /^Cougars|Kane County Cougars|Cougars Baseball$/.test( value ) ) {
  ...
}

回答by Arun Banik

try this,

尝试这个,

var value = document.getElementById('textbox').value;
if (value == 'Cougars' || value == 'Kane County Cougars' || value == 'Cougars Baseball')

回答by 0x499602D2

You're using the ||operator incorrectly. If you want to check if a value equals one of many values, you have to explicitly convey that. For example:

您使用的||运算符不正确。如果您想检查一个值是否等于多个值之一,您必须明确传达这一点。例如:

if ( a == b || a == c || a == d )

Doing it the other way will cause the unexpected behavior you were experiencing.

以另一种方式进行操作会导致您遇到的意外行为。