Arkady Rodestve
Arkady Rodestve

Reputation: 147

Some problem with jquery form

This my code:

$(document).ready(function() {
    $('.submit').click(function() {
        var answer_text = $("#answer_text").val();
        if (answer_text === '' || undefined === $("input[name='answer[scale]']:checked").val()) {
            alert('error!!');
            return false;   
        }
        else {
            alert('yeah! cool baby!');
        }
    }
});

Problem: jQuery doesn't see the ||. I don't know what to do. I tried to do something like:

if

   else if

else

or

if 

  else

    if

    else

Don't know what to do. please help me, maybe some error and mistakes with OR operator? or what?

Upvotes: 2

Views: 59

Answers (4)

Rafay
Rafay

Reputation: 31043

i guess what you wanted to do is

 if (answer_text === '' ||  $("input[name='answer[scale]']:checked").val()==="undefined"){

you have got the operands on the wrong side of the operator

Upvotes: 2

Vinayak Phal
Vinayak Phal

Reputation: 8919

Try to wrap it in proper braces and check.

if ((answer_text === '') || (undefined === $("input[name='answer[scale]']:checked").val()))

Upvotes: 1

Shadow Wizzard
Shadow Wizzard

Reputation: 66398

To know if no checkbox was checked just use the length, no need to mess with the value:

if (answer_text === '' || $("input[name='answer[scale]']:checked").length === 0) {
    //answer text is empty and no answer scale checkbox was checked
}

Upvotes: 2

GuyFromOverThere
GuyFromOverThere

Reputation: 471

    $(document).ready(function() { 
        $('.submit').click(function() { 
                    var answer_text = $("#answer_text").val(); 


                    if (answer_text == ''){
                    if ( undefined === $("input[name='answer[scale]']:checked").val()){ 
                        alert('error!!'); 
                        return false;    
                    } 

                    else{ 
                        alert('yeah! cool baby!'); 

                    } 
                    } 

                    else{ 
                        alert('yeah! cool baby!'); 

                    } 
} 

} 

This is not the fastest way but it will do it...

Upvotes: 0

Related Questions