我有一个变量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;
}
}
答案 0 :(得分:5)
=
是赋值运算符。
==
是比较运算符。
===
是身份运营商。
Take a look on how to compare in JavaScript!
您的代码应为:
if (CampaignType != 2)
{
if (CampaignType == 1)
{
alert(CampaignType);
var CampaignAmount = (SelValue * CampaignPrice) / 100;
SelValue = SelValue - (CampaignAmount);
}
else if (CampaignType == 0')
{
SelValue = SelValue - CampaignPrice;
}
}
答案 1 :(得分:2)
您将值1分配给CampaingType
:
CampaignType = '1'
如果你想比较:
CampaignType == '1'
答案 2 :(得分:1)
你要分配它,执行:
if (CampaignType == '1')
答案 3 :(得分: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