多个If If语句只返回第一个值

时间:2018-01-16 21:52:51

标签: javascript

以下代码仅返回第一个 if语句,无论var product_check是否实际上等于其他条件。有人能指出我正确的方向吗?我确定它必须是一个简单的解决方案......

var product_check = '<?php echo($productLine); ?>';

if (product_check === 'Blades' || 'Ceiling Tiles' || 'Clouds' || 'Partitions' || 'Panels') {
    jQuery('select#input_6_28 option').html('Acoustical Solutions');
} else if (product_check === 'Curva') {
    jQuery('select#input_6_28 option').html('Appliances');
} else if(product_check === 'Acrylic Resin Panels' || 'High Gloss Panels' || 'Super Matte Panels' || 'Modular Cork Wall Panels' || 'Reclaimed Hardwood') {
    jQuery('select#input_6_28 option').html('Architectural Panels');      
}

1 个答案:

答案 0 :(得分:1)

您需要单独检查每一个。

试试这个:

var product_check = '<?php echo($productLine); ?>';

if (product_check === 'Blades' || product_check === 'Ceiling Tiles' || product_check === 'Clouds' || product_check === 'Partitions' || product_check === 'Panels') {

jQuery('select#input_6_28 option').html('Acoustical Solutions');

} else if (product_check === 'Curva') {

    jQuery('select#input_6_28 option').html('Appliances');

} else if(product_check === 'Acrylic Resin Panels' || product_check === 'High Gloss Panels' || 
product_check === 'Super Matte Panels' || product_check === 'Modular Cork Wall Panels' || product_check ==='Reclaimed Hardwood') {

    jQuery('select#input_6_28 option').html('Architectural Panels');
}

使用switch块会更好。

示例:

switch (product_check) {
    case 'Curva':
        jQuery('select#input_6_28 option').html('Appliances');
        break;
    case 'Acrylic Resin Panels':
    case 'High Gloss Panels':
    case 'Super Matte Panels':
    case 'Modular Cork Wall Panels':
    case 'Reclaimed Hardwood':
        jQuery('select#input_6_28 option').html('Architectural Panels');
        break;
    default:
        jQuery('select#input_6_28 option').html('Acoustical Solutions');
        break;
}