2017-11-10 3 views
-2

ORステートメントを使用しようとしていますが、予期しない||jquery ifステートメントで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; 
    } 
}); 

変数を個別に追加および変更するときに機能しますが、ORを使用すると機能しません。どんな入力も感謝します。

ありがとうございます!

+0

これは有効なJSではありません。コンソールのエラーを確認してください。 – bassxzero

+0

あなたは ")"条件をタイプしていません。$(this).attr( "noAddedSugar")!= "undefined")は最初に開く必要があります –

+0

if-syntaxが 'if(expression)' not 'if(expression)| | (式) 'あなたは' if((expression)||(expression)) 'を必要とします。 – user3154108

答えて

1
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") ) 
+0

答えは簡単です。このコードを使用する場合、if((typeof $(this)).attr( "vegan")!= "undefined")||(typeof $(this).attr( "glutenfree")!= "undefined")|| ( "菜食主義者"のタイプ$(this))!= "undefined")){ $( '#diettext')テキスト( "ALAPSZÓSZNÉLKÜLKÉRD!"); } else { $( '#diettext')。text( ""); $( '#diettext')。fadeTo(1000、0); } ステートメントのIF部分はうまく機能しますが、私が何をするにしても、else部分を機能させることはできません。それを見てもらえますか? https://codepen.io/Pbalazs89/full/LOjqQM – Pbalazs89

1

あなたのロジックは完全に括弧の別のセットで囲む必要がある場合でなければなりません:

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

それが今ある道を、それが最初!="undefined")後に終了

各条件の内側のかっこは必要ではないので、簡単にすることができます

if(typeof $(this).attr("noAddedSugar") != "undefined" || typeof $(this).attr("vegan") != "undefined" || typeof $(this).attr("glutenfree") != "undefined") 
関連する問題