A jQuery 'if' condition to check multiple values

后端 未结 4 1539
长发绾君心
长发绾君心 2021-01-18 14:55

In the code below, is there a better way to check the condition using jQuery?

if(($(\'#test1\').val() == \'first_value\')||($(\'#test2\').val() == \'second_v         


        
4条回答
  •  萌比男神i
    2021-01-18 15:30

    Demo: just another idea is at http://jsfiddle.net/h3qJB/. Please let me know how it goes.

    You can also do chaining like:

    $('#test1, #test2, #test3, #test4').each(function(){ //...use this.value here  });
    

    It might be that De Morgan's laws gives you an idea of how to make the logic a bit more compact (although I am not sure what is the specific case or is it as simple as comparing values).

    Code

    var boolean1 = (($('#test1').val() == 'first_value')||($('#test2').val() == 'second_value'))
    
    var boolean2 = (($('#test3').val()!='third_value')|| ($('#test4').val()!='fourth_value'))
    
    if (boolean1 && boolean2)
        alert("bingo");
    else
        alert("buzzinga");
    

提交回复
热议问题