A jQuery 'if' condition to check multiple values

后端 未结 4 1500
长发绾君心
长发绾君心 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条回答
  •  迷失自我
    2021-01-18 15:36

    var c=0, b='#test', a=['first_value','second_value','third_value','fourth_value'];
    for(var i=0; i<4; i++)
        if($(b+i).val() == a[i])
            c=1;
    if (c) //Do stuff here
    

    This will decrease your code size by 25 bytes;-)

提交回复
热议问题