javascript 带有 if 语句的用户文本输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15591829/
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
User text input with an if statement
提问by Nash
I'm new to JavaScript and have run into a challenge (for me). I've looked online for a solution, but to my surprise I couldn't find it. This must have a very basic solution!
我是 JavaScript 的新手,遇到了挑战(对我来说)。我已经在网上寻找解决方案,但令我惊讶的是我找不到它。这必须有一个非常基本的解决方案!
The problem: I want JavaScript to execute an if statement based on user input from a text field.
问题:我希望 JavaScript 根据来自文本字段的用户输入执行 if 语句。
<input id="text_a" type="text;" />
<p id="answer"></p>
<button onclick="myFunction()">Check</button>
<script>
function myFunction()
{
var a=document.getElementById("text_a").vlaue;
if (a=="hello")
{
document.getElementById("answer").innerHTML="Correct!";
}
else
{
document.getElementById("answer").innerHTML="Oops!";
}
}
</script>
When I try this creation out, no matter what is entered into the text input (even the right answer) the else statement is executed. What am I missing to get the if statement to execute?? Thanks in advance for any and all help
当我尝试这个创建时,无论在文本输入中输入什么(即使是正确的答案),都会执行 else 语句。我缺少什么才能执行 if 语句?在此先感谢您的任何帮助
采纳答案by Sachin
回答by George
You've spelt value wrong:
你拼错了 value:
var a=document.getElementById("text_a").vlaue;
Should be
应该
var a=document.getElementById("text_a").value;
回答by Steven
You misspelled value.
你拼错了价值。
var a=document.getElementById("text_a").vlaue;