错误比较Javascript中的两个字符串

时间:2015-04-03 12:54:46

标签: javascript extjs

我使用以下java控制器制作了以下脚本。 在控制器中我设置了uimodel的属性,这个属性是一个id,我把它转换成一个字符串。 之后,在我的脚本中,我取一个变量中的值,然后打印这个变量的类型,它是一个字符串,值为1。 但是当我将它与线条进行比较时:

if (profile.toString() != "1" || profile.toString() != "2") {
    alert("pas manager");    
}

显示警报,但ID配置文件等于1,因此不应显示警报! 我尝试使用数字,但它也不起作用

Controller Java:

uiModel.addAttribute(WebConstants.PROFIL_ID, user.getProfile().getId().toString()); 

JS脚本:

<script type="text/javascript">
 Ext.onReady(function() {
 var profile = ${profilId};
 alert(typeof profile.toString() + profile);
 alert(typeof "1" + "1");
 if (profile.toString() != "1" || profile.toString() != "2") {
     alert("pas manager");
     App.removeOption(Ext.getElementById("site.country.zone.region.code"));
     App.populateTargetListForWorkflow("", Ext.getElementById("site.country.zone.region.code"), "region"); 
}
else {
    alert("manager");
    Ext.getElementById("site.country.zone.region.code").options[0].value = "EUATEST";
}
});
</script>

有没有人有解决方案来解决这个问题?

2 个答案:

答案 0 :(得分:2)

你有一个逻辑问题。

if (a != "1" || a != "2") {

总是经过验证,无论a的价值如何,因为a不能同时等同于12

你似乎想要

if (profile.toString() != "1" && profile.toString() != "2") {

答案 1 :(得分:-3)

如果您提醒显示个人资料ID为“1”,请更改以下代码。

这里我将!=更改为!==。有关详情,请 Click here

<script type="text/javascript">
     Ext.onReady(function() {
     var profile = ${profilId};
     alert(typeof profile.toString() + profile);
     alert(typeof "1" + "1");
     if (profile.toString() !== "1" || profile.toString() !== "2") {
         alert("pas manager");
         App.removeOption(Ext.getElementById("site.country.zone.region.code"));
         App.populateTargetListForWorkflow("", Ext.getElementById("site.country.zone.region.code"), "region"); 
    }
    else {
        alert("manager");
        Ext.getElementById("site.country.zone.region.code").options[0].value = "EUATEST";
    }
    });
    </script>
相关问题