mac3201
mac3201

Reputation: 85

Multiple conditions need to be met using if statements in jQuery

Can I have multiple conditions in an if statement using jQuery? The following code is not catching the errors at all. made edits per posted suggestions

 $('.goRedIMG').on('click',function(event){
        var ischecked = false;
        var isOKtoSubmit = true;
        var alertMessage = 'No tools have been selected';
        var statusvar = '';
        var transferstatusvar = '';
        var action = $('#uTransaction option:selected').html();

        $('.chkaction').each(function() { //loop through each checkbox
            statusvar = $(this).closest('tr').children('.recordStatus').html();
            transferstatusvar = $(this).closest('tr').children('.transferstat').html()

            if($(this).prop('checked')) {
                ischecked = true;
                if((action == 'Transfer' && statusvar != 'ACTIVE') || (transferstatusvar != 'OK')){             
                    isOKtoSubmit = false;
                    alertMessage = 'One or more records cannot be transferred because status is not ACTIVE and transfer status is not OK';           
                }                  
            }
            if(action == 'Accept Transfer' && transferstatusvar != 'AWAITING'){                
                    isOKtoSubmit = false;
                    alertMessage = 'One or more records cannot be accepted or rejected because status is not ACTIVE or Transfer Status is not AWAITING';                    
                }              
       });
    if(isOKtoSubmit && ischecked !== false && action !== '--Select One--'){                 
            $('#toolActions').submit();         
        }else {
            alert(alertMessage);
        }       
    });

Upvotes: 2

Views: 18757

Answers (1)

Control Freak
Control Freak

Reputation: 13213

When you're using && (AND) and || (OR) in the same if argument, you need to group them.

So instead of this:

if(action == 'Transfer' && statusvar !== 'ACTIVE' || (transferstatusvar !== 'OK'))

You should do:

if((action == 'Transfer' && statusvar != 'ACTIVE') || (transferstatusvar != 'OK'))

(Also, you don't need !== when you want to check if not equals, you do != instead)

Upvotes: 4

Related Questions