-
郎朗坤
使用正则表达式:if (/^(something|nothing|anything|everything)$/.exec('jesus')) alert('Who cares?');或相反:/^(something|nothing|anything|everything)$/.exec('jesus')||alert('Who cares?');[更新]更短;-)if (/^(some|no|any|every)thing$/.exec('jesus')) alert('Who cares?');
-
开满天机
你可以用这个...if (["something", "nothing", "anything", "everything"].includes(a)) { alert('Who cares?');}如果您对旧版浏览器的支持受阻...if (["something", "nothing", "anything", "everything"].indexOf(a) > -1) { alert('Who cares?');}您也将其标记为jQuery,因此,如果您需要不带jQuery的旧浏览器Array.prototype.indexOf(),则可以使用$.inArray()。
-
萧十郎
您可以将选项放入数组中,并使用jQuery $.inArray()或javascrpt indexOf()搜索数组纯JavaScript var a = 'anything';arr = ['something', 'nothing', 'anything', 'everything'];if(arr.indexOf(a) != -1) alert("condition met"); else alert("condition not met"); 使用jQueryvar a = 'jesus';arr = ['something', 'nothing', 'anything', 'everything'];if($.inArray(a, arr) != -1) // With jQuery alert("condition met"); else alert("condition not met");