javascript - "else if" is not working in jQuery? - Stack Overflow

I have written an if statement in jQuery.My code:if($('#customer0').checked()){$('#shop

I have written an if statement in jQuery.

My code:

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

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

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

When I have if (custmer0) and else (customer1) the fadeOut and fadeIn work good. But when it's customer2 (that is else statement) it is not working? What is wrong?

I have written an if statement in jQuery.

My code:

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

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

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

When I have if (custmer0) and else (customer1) the fadeOut and fadeIn work good. But when it's customer2 (that is else statement) it is not working? What is wrong?

Share Improve this question edited Mar 28, 2011 at 16:00 Carl 1,2639 silver badges24 bronze badges asked Mar 28, 2011 at 14:41 softwaresoftware 7286 gold badges18 silver badges39 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

else doesn't take any parameters. The code below is more applicable:

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

On your third else

Change

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

to

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

fix you parens:

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

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

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

The reason why you code want working is the if statement was taking the wrong else for when the if statement is false

Also you cannot have params for an else statement

First off, the syntax for getting the value of a checkbox is:

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

Second, else statements don't have (statement) after them, you need to use a else if.

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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744245412a4564903.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信