Javascript 在jQuery if语句中使用OR运算符时出现问题

Javascript 在jQuery if语句中使用OR运算符时出现问题,javascript,jquery,conditional-statements,Javascript,Jquery,Conditional Statements,我的目标是使用OR语句列表检查给定成分的不同属性,但它返回意外的| |错误 $(".ingredient.clicked").each(function() { if (typeof $(this).attr("noAddedSugar") != "undefined") || (typeof $(this).attr("vegan") != "undefined")

我的目标是使用OR语句列表检查给定成分的不同属性,但它返回意外的| |错误

$(".ingredient.clicked").each(function() {
  if (typeof $(this).attr("noAddedSugar") != "undefined") 
      || (typeof $(this).attr("vegan") != "undefined") 
      || (typeof $(this).attr("glutenfree") != "undefined")  
  {
    $('#proba').text("Proba");
  } else {
    $('#proba').text("");
    return false;
  }  
});
它在我单独添加和修改变量时有效,而在我使用或时无效。如有任何意见,将不胜感激

谢谢

   if (typeof $(this).attr("noAddedSugar") != "undefined") || (typeof $(this).attr("vegan") != "undefined") || (typeof $(this).attr("glutenfree") != "undefined") 
应该是

if( (typeof $(this).attr("noAddedSugar") != "undefined") || (typeof $(this).attr("vegan") != "undefined") || (typeof $(this).attr("glutenfree") != "undefined")  )

您的if逻辑需要完全包含在一组单独的括号中:

 if ((typeof $(this).attr("noAddedSugar") != "undefined") || (typeof $(this).attr("vegan") != "undefined") || (typeof $(this).attr("glutenfree") != "undefined"))
按照现在的方式,它在第一个
之后结束=“未定义”)

每个条件周围的内圆括号不是必需的,因此您可以使用以下方法将其简化一点:

if( typeof $(this).attr("noAddedSugar") != "undefined" || typeof $(this).attr("vegan") != "undefined" || typeof $(this).attr("glutenfree") != "undefined" )

这是无效的JS。检查控制台是否有错误。您已取消关闭“)”条件,typeof$(this).attr(“noAddedSugar”)!=如果语法是
if(expression)
而不是
if(expression)| |(expression)
则需要首先打开“undefined”),因此需要
if((expression)| |(expression))
关于答案的快速问题。如果我使用这个代码-If($(typeof$(this.attr)(“素食者”)!=“未定义的”)|(typeof$(this.attr)(“麸质自由”)!=“未定义的”)|(typeof$(this.attr)(“素食者”)!“未定义的”){$('dietext').text('ALAPSZÓSZ NÉLK L KÉRD!”)}否则{$('dietext text('dietext语句的IF部分工作得很好,但不管我做什么,我都无法让else部分发挥作用。你介意看一下吗?