-4

我有一个变量 CampaignType,它的值为 0。但在警报中(双星内)它变为 1。为什么会这样?这是我的 javascript 代码片段

      if (CampaignType != 2) 
      {
       if (CampaignType = '1') 
        {
            **alert(CampaignType);**
            var CampaignAmount = (SelValue * CampaignPrice) / 100;
            SelValue = SelValue - (CampaignAmount);

        }
        else if (CampaignType = '0')
        {
            SelValue = SelValue - CampaignPrice;

        }
    }
4

4 回答 4

5

=是赋值运算符。

==是比较运算符。

===是身份运算符。

看看如何在 JavaScript 中进行比较!

您的代码应该是:

if (CampaignType != 2) 
  {
   if (CampaignType == 1) 
    {
        alert(CampaignType);
        var CampaignAmount = (SelValue * CampaignPrice) / 100;
        SelValue = SelValue - (CampaignAmount);

    }
    else if (CampaignType == 0')
    {
        SelValue = SelValue - CampaignPrice;

    }
}
于 2012-08-10T11:58:57.637 回答
2

您将值 1 分配给CampaingType

CampaignType = '1'

如果要比较:

CampaignType == '1'
于 2012-08-10T11:58:39.327 回答
1

您正在分配它,请执行以下操作:

if (CampaignType == '1') 
于 2012-08-10T11:57:34.907 回答
1

那是因为您不是在评估而是在设置一个值

利用

if (CampaignType === '1') //if you also want to verify they are the same type

if (CampaignType == '1') //if you do not want to verify if they are the same type
于 2012-08-10T11:57:41.810 回答