如何在 jquery 中的 if 和 else 中进行数学运算?

发布于 2024-10-31 11:30:55 字数 1408 浏览 1 评论 0原文

大家好!

我一直在尝试在 jQuery 中创建 if/else 语句代码,但我猜我的 if/else code> 声明安息!我不知道出了什么问题,我已经做了一切,但仍然无法正常工作!好的,这是我的问题列表 ~

  1. 我的 if/else 变得相反!
  2. 我认为一切都变得一团糟!
  3. 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 元素中检索值!但它不起作用。

请看这里 ----

问题演示 2

< 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 ~

  1. My if/else is getting inverse!
  2. And I think everything is just getting messed up!
  3. JsLint (in jsFiddle.net) is showing no error in my jQuery code!

Please here is my problem demo link ~~~~~~~~~~

PROBLEM DEMO

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 ----

PROBLEM DEMO 2

THANKS IN ADVANCE (for this one too!)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

九八野马 2024-11-07 11:30:55

问题在于 正确不正确 是字符串。您可以将它们更改为整数来解决您的问题:

                  var correct = 10;
                  var incorrect = 9;

或者,您可以使用 parseInt(myNumAsString, 10) 在运行时将字符串转换为数字。

The problem is that correct and incorrect are strings. You can change them to integers to fix your problem:

                  var correct = 10;
                  var incorrect = 9;

Alternatively, you can use parseInt(myNumAsString, 10) to convert a string to a number at runtime.

唠甜嗑 2024-11-07 11:30:55

您需要删除正确不正确变量周围的单引号。目前,它们被视为字符串,而不是整数。

var correct = 10;
var incorrect = 9;

http://jsfiddle.net/aj49m/1/

You need to remove the single-quotes from around your correct and incorrect variables. Currently, they are being treated as strings, not integers.

var correct = 10;
var incorrect = 9;

http://jsfiddle.net/aj49m/1/

山有枢 2024-11-07 11:30:55

您正在比较字符串。它应该是这样的(不带引号):

var correct = 10;
var incorrect = 9;

You are comparing strings. It should be like this (without quotes):

var correct = 10;
var incorrect = 9;
几度春秋 2024-11-07 11:30:55

修复使用整数而不是字符串:

http://jsfiddle.net/aj49m/2/

Fixed use integers instead of strings:

http://jsfiddle.net/aj49m/2/

破晓 2024-11-07 11:30:55

您正在比较字符串文字“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文