Javascript If 语句不适用于 And (&&) 运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2204767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:14:14  来源:igfitidea点击:

If Statement not working with And (&&) Operator

javascriptcontent-management-systemcdatalogical-operatorssquarespace

提问by VUELA

I'm having a hard time writing up what seems should be a simple if statement! I need it to say if mod does not equal a, b, or c - then do this. Here is what I was trying but have been unsuccessful:

我很难写出看似简单的 if 语句!我需要它说如果 mod 不等于 a、b 或 c - 然后执行此操作。这是我尝试但未成功的方法:

var mod = CURRENT_MODULE_ID;
if (mod != "5827289" && mod != "5195103" && mod != "5181422") {
   doSomething();
}

When I type this into my editor it says there is an error, specifically that "The entity name must immediately follow the '&' in the entity reference." .. and is not working when I go to test.

当我在编辑器中输入它时,它说有一个错误,特别是“实体名称必须紧跟在实体引用中的 '&' 之后。” ..并且在我去测试时不起作用。

Any help is appreciated!!

任何帮助表示赞赏!



UPDATE: The url: esber.squarespace.com

更新:网址:esber.squarespace.com

The full script:

完整脚本:

<script type="text/javascript" src="/storage/scripts/sessvars.js"></script>
<script type="text/javascript">
<![CDATA[ 

onload=function(){
 sessvars.browserConfirmation?'none':'';
 sessvars.ageConfirmation?'none':'';
}; 

var mod = Squarespace.Constants.CURRENT_MODULE_ID;
if (mod != "5827289" && mod != "5195103" && mod != "5181422") {
   if(sessvars.ageConfirmation != "yes"){
      window.location = "/verify/";
   };
};

]]>
</script> 

I want every page in the site to automatically redirect on page load to the verify page, unless it is the verify page (/verify), the "You are not verified" page (/not-verified), or the login page (/login) -- unless the user already verified by setting the sessvars, then they can continue on to the homepage.

我希望网站中的每个页面在页面加载时自动重定向到验证页面,除非它是验证页面 (/verify)、“您未验证”页面 (/not-verified) 或登录页面 (/ login) -- 除非用户已经通过设置 sessvars 进行了验证,否则他们可以继续访问主页。

To test this I go to esber.squarespace.com and click on one the menu items at the right (this menu would eventually be hidden when I'm done with the page) -- when i try to go to another page without veriying my age first i should be redirected back to the /verify page but that isnt happening.

为了测试这一点,我转到 esber.squarespace.com 并单击右侧的菜单项之一(当我完成页面后,此菜单最终将被隐藏)-当我尝试转到另一个页面而不验证我的首先我应该被重定向回 /verify 页面,但这并没有发生。

If i revise the script to:

如果我将脚本修改为:

<script type="text/javascript" src="/storage/scripts/sessvars.js"></script>
<script type="text/javascript">

onload=function(){
 sessvars.browserConfirmation?'none':'';
 sessvars.ageConfirmation?'none':'';
}; 

var mod = Squarespace.Constants.CURRENT_MODULE_ID;
if (mod != "5827289") {
   if(sessvars.ageConfirmation != "yes"){
      window.location = "/verify/";
   };
};

</script> 

then it works fine(?)

然后它工作正常(?)

回答by Waleed Al-Balooshi

Try this:

尝试这个:

// <![CDATA[ 

onload=function(){
 sessvars.browserConfirmation?'none':'';
 sessvars.ageConfirmation?'none':'';
}; 

var mod = Squarespace.Constants.CURRENT_MODULE_ID;
if (mod != "5827289" && mod != "5195103" && mod != "5181422") {
   if(sessvars.ageConfirmation != "yes"){
      window.location = "/verify/";
   };
};

// ]]>

If this doesn't work, just leave the code there for a bit, so that we can debug it directly on your website

如果这不起作用,只需将代码留在那里,以便我们可以直接在您的网站上进行调试

回答by Lachlan Roche

Wrap your script in a CDATA section.

将您的脚本包装在 CDATA 部分中。

<script type="text/javascript">
<![CDATA[

// script here

]]>
</script>

回答by Martin Booth

Are you embedding this javascript in an xml document?

您是否将此 javascript 嵌入到 xml 文档中?

It sounds like the xml document is not well formed, perhaps because the & should be escaped as &

听起来 xml 文档格式不正确,可能是因为 & 应该转义为 &

The javascript by itself looks fine too me

javascript 本身看起来也不错我

Try:

尝试:

var mod = CURRENT_MODULE_ID;
if (mod != "5827289" &amp;&amp; mod != "5195103" &amp;&amp; mod != "5181422") {
   doSomething();
}

You'll find out that way whether the javasciprt needs to be escaped

您将通过这种方式找出是否需要转义 javasciprt

Edit in response to comment:

编辑以回应评论:

Try the following:

请尝试以下操作:

<script type="text/javascript">
<![CDATA[
var mod = CURRENT_MODULE_ID;
if (mod != "5827289" && mod != "5195103" && mod != "5181422") {
   doSomething();
}
]]>
</script>

回答by o.k.w

I tried the EXACT same code as yours and it works fine:

我尝试了与您完全相同的代码,它工作正常:

function doSomething() {alert("doing");}
var CURRENT_MODULE_ID = 5195103000;
var mod = CURRENT_MODULE_ID;
if (mod != "5827289" && mod != "5195103" && mod != "5181422") {
   doSomething();
}

It did 'doSomething'. When value is changed to 5195103, nothing happens which is correct

它确实“做了一些事情”。当值更改为 5195103 时,没有任何反应,这是正确的

The editor aside, what's the script error when you run it and what's the browser you used? I suspect it could be an error elsewhere or perhaps related to CURRENT_MODULE_ID?

撇开编辑器不谈,运行时出现的脚本错误是什么?您使用的是什么浏览器?我怀疑这可能是其他地方的错误或可能与CURRENT_MODULE_ID?

回答by Rex M

It sounds like your editor just thinks you're working with an XML document. Have you tried actually running this in a browser? If so, does the browser also give an error?

听起来您的编辑器只是认为您正在处理 XML 文档。您是否尝试过在浏览器中实际运行它?如果是这样,浏览器是否也会报错?

回答by Mottie

Are you trying to compare the ID as a string or value? Did you try it without quotes?

您是否尝试将 ID 作为字符串或值进行比较?你试过没有引号吗?

var mod = CURRENT_MODULE_ID;
if (mod != 5827289 && mod != 5195103 && mod != 5181422) {
   doSomething();
}

or another method would be to use match

或另一种方法是使用匹配

var mod = CURRENT_MODULE_ID;
if (!mod.match("5827289|5195103|5181422")) {
   doSomething();
}

回答by D. Kermott

I got this error within a script section in an XSL file.

我在 XSL 文件的脚本部分中收到此错误。

Entity '&' not defined

实体“&”未定义

I adapted the above answer within my script and it worked.

我在我的脚本中修改了上述答案并且它起作用了。

Note the CDATA section in the code segment below

注意下面代码段中的 CDATA 部分

<script>
  var  Quantity860=<xsl:value-of select="$QuantityOrdered_860" />;
  var  Quantity850=<xsl:value-of select="$QuantityOrdered_850" />;
  var  QuantityToReceive860=<xsl:value-of select="$QuantityLeftToReceive_860" />;

  if(parseFloat(Quantity860.textContent) !== parseFloat(Quantity850.textContent) <![CDATA[ && ]]> parseFloat(QuantityToReceive860.textContent) !== parseFloat(Quantity850.textContent))
  {
      Quantity860.style.color="#FF6347";
      Quantity850.style.color="#FF6347";
      QuantityToReceive860.style.color="#FF6347";
  }
</script>