2011-03-28 3 views
1

J'ai écrit une instruction if dans jQuery."else if" ne fonctionne pas dans jQuery?

Mon code:

if($('#customer0').checked()) 
{ 
    $('#shop').fadeOut(); 
} 

else if($('#customer1').checked()) 
{ 
    $('#shop').fadeIn(); 
} 

else ($('#customer2').checked()) 
{ 
    $('#shop').fadeOut(); 
}; 

Quand j'ai if (custmer0) et else (customer1) le fadeOut et fadeIn bon travail. Mais quand c'est customer2 (c'est autre chose) ça ne marche pas? Qu'est-ce qui ne va pas?

Répondre

5

sinon ne prend aucun paramètre. Le code ci-dessous est plus applicable:

if($('#customer0').checked()) { $('#shop').fadeOut(); } 
else if($('#customer1').checked()) { $('#shop').fadeIn(); } 
else if($('#customer2').checked()) { $('#shop').fadeOut(); }; 
0

fix vous parens:

if ($('#customer0').checked()) { 
    $('#shop').fadeOut(); 
} 

else { 
    if ($('#customer1').checked()) { 
     $('#shop').fadeIn(); 
    } 

    else { 
     if($('#customer2').checked()) { 
      $('#shop').fadeOut(); 
     } 
    } 
} 

La raison pour laquelle vous codez voulez travailler est l'instruction if a fait fausse route autre quand l'instruction if est false

vous pouvez également avoir des params pour un else instruction

+0

@George Bailey, j'ai fixé cela^_ ^ – Neal

1

Sur votre troisième autre

changement

else ($('#customer2').checked()) 

à

else if ($('#customer2').checked()) 
0

Tout d'abord, la syntaxe pour obtenir la valeur d'une case à cocher est:

$('#customer0').attr('checked') 

Deuxièmement, else instructions n'ont pas (statement) après eux, vous devez utiliser un else if.

if($('#customer0').attr('checked')) 
{ 
    $('#shop').fadeOut(); 
} 

else if($('#customer1').attr('checked')) 
{ 
    $('#shop').fadeIn(); 
} 

else if($('#customer2').attr('checked')) 
{ 
    $('#shop').fadeOut(); 
} 
Questions connexes