JavaScript - 如果 #id 等于某物
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14027428/
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
JavaScript - if #id equals something
提问by JoseBazBaz
Why doesn't the following code work?
为什么下面的代码不起作用?
var err = document.getElementById("text-error").value;
if (err == "Team already exists") {
$('#text-error').fadeIn(400).delay(3200).fadeOut(800);
}
The error does not fade in or fade out.
错误不会淡入或淡出。
Checked the console - no problems.
检查控制台 - 没有问题。
Also, this particular error is sent via the server.
此外,此特定错误是通过服务器发送的。
The fade in and fade out work for my client side errors - but not errors pertaining to my database like this one - if that makes any difference to the problem.
淡入和淡出适用于我的客户端错误 - 但不是与我的数据库相关的错误 - 如果这对问题有任何影响。
UPDATE
更新
After console logging - I realize that it does not enter the if statement, even though it clearly equals it - via html code and via a quick glance at the page.
在控制台记录之后 - 我意识到它没有输入 if 语句,即使它显然等于它 - 通过 html 代码和快速浏览页面。
回答by Omiga
Try :
尝试 :
var err = $("#text-error").val();
if (err == "Team already exists") {
console.log('is equal but problem is fadeing not working');
$('#text-error').fadeIn(400).delay(3200).fadeOut(800);
}else {
console.log('not equal');
}
but ofcourse more details about the issue will help us
但当然有关该问题的更多详细信息将有助于我们
回答by Infinity
I am assuming you have something like
我假设你有类似的东西
<div id="text-error> Team already exists </div>
If so, then replace your first line of code with
如果是这样,则将您的第一行代码替换为
var err = document.getElementById("text-error").innerText;
JsFiddle - http://jsfiddle.net/fCNe8/
JsFiddle - http://jsfiddle.net/fCNe8/