If 语句不能与 And (&&) 运算符一起使用

发布于 2024-08-20 20:58:09 字数 1631 浏览 12 评论 0原文

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

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

当我在编辑器中输入此内容时,它说存在错误,特别是“实体名称必须立即跟在‘&’之后在实体参考中。” ..并且当我去测试时不起作用。

任何帮助表示赞赏!


更新: 网址:esber.squarespace.com

完整脚本:

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

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

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

如果我将脚本修改为:

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

那么它就可以正常工作(?)

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:

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

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.

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.

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(?)

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

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

发布评论

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

评论(8

柠檬色的秋千 2024-08-27 20:58:09

试试这个:

// <![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/";
   };
};

// ]]>

如果这不起作用,请将代码留在那里一会儿,以便我们可以直接在您的网站上调试它

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

紫轩蝶泪 2024-08-27 20:58:09

将脚本包装在 CDATA 部分中。

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

// script here

]]>
</script>

Wrap your script in a CDATA section.

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

// script here

]]>
</script>
话少情深 2024-08-27 20:58:09

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

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

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

除了编辑器之外,运行它时的脚本错误是什么以及您使用的浏览器是什么?我怀疑这可能是其他地方的错误或者可能与 CURRENT_MODULE_ID 相关?

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

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 ?

橙幽之幻 2024-08-27 20:58:09

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

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

javascript本身看起来也很好我

尝试:

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

你会发现这样是否需要转义javasciprt

编辑回应评论:

尝试以下操作:

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

Are you embedding this javascript in an xml document?

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

The javascript by itself looks fine too me

Try:

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

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

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>
小…红帽 2024-08-27 20:58:09

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

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?

深者入戏 2024-08-27 20:58:09

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

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

或者另一种方法是使用 match

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

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

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();
}
抹茶夏天i‖ 2024-08-27 20:58:09

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

实体“&”未定义

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

注意下面代码段中的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>

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

Entity '&' not defined

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

Note the CDATA section in the code segment below

<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>
谁的新欢旧爱 2024-08-27 20:58:09

只需使用 != 进行比较,而不是 == 然后 &&将工作

if(val != "" && val != "") {

console.log("filled");

}else
{console.log("空"); }

just use != in comparison instead of == then && will work

if(val != "" && val != "") {

console.log("filled");

}else
{console.log("empty"); }

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