我在JavaScript中有这个条件,目前它所做的是在订单状态准备好发货时加载下拉列表。我还希望if条件适用于OR Dispatched。
if条件低于
if ($(this).closest("td").closest("tr").find("input.pcss-product-order-status").val() == "ReadyForDespatch")
如何添加或为此|| "Despatched"
就像是
if ($(this).closest("td").closest("tr").find("input.pcss-product-order-status").val() == "ReadyForDespatch" || "Despatched")
由于
答案 0 :(得分:5)
答案 1 :(得分:3)
六个选项:
// KISS solution ;)
var val = $(this).closest("td").closest("tr").find("input.pcss-product-order-status").val();
if(val == "ReadyForDespatch" || val == "Despatched") {
}
// ES6+
if(["ReadyForDespatch", "Despatched"].includes($(this).closest("td").closest("tr").find("input.pcss-product-order-status").val())) {
}
// ES5+
var val = $(this).closest("td").closest("tr").find("input.pcss-product-order-status")).val();
if(["ReadyForDespatch", "Despatched"].some(someValue => someValue == val)) {
}
// ES5+
if(["ReadyForDespatch", "Despatched"].indexOf($(this).closest("td").closest("tr").find("input.pcss-product-order-status")).val()) > -1) {
}
// ES ALL
switch($(this).closest("td").closest("tr").find("input.pcss-product-order-status").val())
{
case "ReadyForDespatch":
case "Despatched":
break;
}
// ES6+
// This is an overkill for 2 elements, but I've added it for the sake of
// showing the wonders of Set collection
if(new Set(["ReadyForDespatch", "Despatched"]).has($(this).closest("td").closest("tr").find("input.pcss-product-order-status").val()) {
}