一个jQuery'if'条件来检查多个值 [英] A jQuery 'if' condition to check multiple values

查看:119
本文介绍了一个jQuery'if'条件来检查多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,有没有更好的方法来使用jQuery检查条件?

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

if(($('#test1').val() == 'first_value')||($('#test2').val() == 'second_value') && ($('#test3').val()!='third_value')|| ($('#test4').val()!='fourth_value'))


推荐答案

除非有其他问题,比如你是否会重复使用#test1,...字段进行更多处理,你的应该是好的。

Unless there are other concerns, like if you will reuse the #test1, ... fields for more processing, yours should be good.

如果您再次获取任何值以执行某些操作,我建议将$('#test1')结果存储在变量中,这样您就不需要重新查询dom。

If you will fetch any of the values again to do something I would recommend storing the $('#test1') result in a variable so that you do not need to requery the dom.

例如:

var t1 = $('#test1');
if((t1.val() == 'first_value')||($('#test2').val() == 'second_value') && ($('#test3').val()!='third_value')|| ($('#test4').val()!='fourth_value')) {
    t1.val('Set new value');
}

这也提高了行的可读性;)

This also improves readability of the row ;)

这篇关于一个jQuery'if'条件来检查多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆