A jQuery 'if' condition to check multiple values

后端 未结 4 1544
长发绾君心
长发绾君心 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:34

    var values = ['first_value', 'second_value', 'third_value', 'fourth_value'];
    $('#test1, #test2, #test3, #test4').each(function(index, el) {
       if($.inArray(this.value, values)) {
         // do some job;
         return false; // or break;
       }
    });
    

提交回复
热议问题