如何在 jquery 中的 if 和 else 中进行数学运算?
大家好!
我一直在尝试在 jQuery
中创建 if/else
语句代码,但我猜我的 if/else
code> 声明安息!我不知道出了什么问题,我已经做了一切,但仍然无法正常工作!好的,这是我的问题列表 ~
- 我的
if/else
变得相反! - 我认为一切都变得一团糟!
- JsLint(在 jsFiddle.net 中)在我的 jQuery 代码中没有显示错误!
请这是我的问题演示链接 ~~~~~~~~~~
这是我的 smaple jQuery
代码~~~~~~~~~
$(function () {
var correct = '10';
var incorrect = '9';
$('div.correct_incorrect').css("background",function () {
if( correct > incorrect )
{
return "#796";
}
else if( correct == incorrect )
{
return "#345";
}
else
{
return "#732";
}
});
});
请帮助我!
提前致谢
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
抱歉打扰大家,但我之前的问题已经解决了,但我这段代码遇到了新问题。在这里,我尝试从两个 input
元素中检索值!但它不起作用。
请看这里 ----
< strong>提前致谢(这也是!)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Hello guys!
I have been trying to create a if/else
statement code in jQuery
but I guess my if/else
statement is resting in peace! I don't know what is getting wrong I have did everything and still it is not working correctly! Ok, here is my problem list ~
- My
if/else
is getting inverse! - And I think everything is just getting messed up!
- JsLint (in jsFiddle.net) is showing no error in my jQuery code!
Please here is my problem demo link ~~~~~~~~~~
Here is my smaple jQuery
code ~~~~~~~~~
$(function () {
var correct = '10';
var incorrect = '9';
$('div.correct_incorrect').css("background",function () {
if( correct > incorrect )
{
return "#796";
}
else if( correct == incorrect )
{
return "#345";
}
else
{
return "#732";
}
});
});
Please help me out!
THANKS IN ADVANCE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sorry to bother guys but my previous question is solved but I'm having a new problem with this code. Here I'm trying to retrive a value from two input
elements! But it is not working.
Please have a look here ----
THANKS IN ADVANCE (for this one too!)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题在于
正确
和不正确
是字符串。您可以将它们更改为整数来解决您的问题:或者,您可以使用
parseInt(myNumAsString, 10)
在运行时将字符串转换为数字。The problem is that
correct
andincorrect
are strings. You can change them to integers to fix your problem:Alternatively, you can use
parseInt(myNumAsString, 10)
to convert a string to a number at runtime.您需要删除
正确
和不正确
变量周围的单引号。目前,它们被视为字符串,而不是整数。http://jsfiddle.net/aj49m/1/
You need to remove the single-quotes from around your
correct
andincorrect
variables. Currently, they are being treated as strings, not integers.http://jsfiddle.net/aj49m/1/
您正在比较字符串。它应该是这样的(不带引号):
You are comparing strings. It should be like this (without quotes):
修复使用整数而不是字符串:
http://jsfiddle.net/aj49m/2/
Fixed use integers instead of strings:
http://jsfiddle.net/aj49m/2/
您正在比较字符串文字“10”和“9”,而不是整数值 10 和 9。
比较字符串时,“9”大于“10”。
You're comparing string literals '10' and '9' instead of integer values 10 and 9.
'9' is bigger than '10' when comparing strings.