Closed. This question is not reproducible or was caused by typos。它当前不接受答案。
                        
                    
                
            
        
            
        
                
                    
                
            
                
                    想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                
                    5年前关闭。
            
        

    

我已经尝试了一段时间。由于某种原因,

否则if(customerType =“ T”)

即使customerType不等于“ T”也会继续执行,所以无论输入什么,除非customerType等于“ R”,“ C”或“ T”,否则DiscountPercent都会变为.40。谁能帮我吗?

var $ = function (id) {
return document.getElementById(id);
}

var calculate_click = function () {
var customerType = $("type").value.toUpperCase;
var invoiceSubtotal = parseFloat( $("subtotal").value );
$("subtotal").value = invoiceSubtotal.toFixed(2);
var discountPercent = .0;
var valid = false;

if (customerType == "R") {
    valid = true;
    if (invoiceSubtotal < 100){
        discountPercent = .0;
    }
    else if (invoiceSubtotal >= 100 && invoiceSubtotal < 250){
        discountPercent = .1;
    }
    else if (invoiceSubtotal >= 250 && invoiceSubtotal < 500){
        discountPercent = .25;
    }
    else if (invoiceSubtotal >= 500){
        discountPercent = .30;
    }
}

else if (customerType == "C") {
    valid = true;
    discountPercent = .20;
    }

else if (customerType = "T"){
    valid = true;
    if(invoiceSubtotal < 500){
        discountPercent = .40;
    }
    if(invoiceSubtotal >= 500){
        discountPercent = .50;
    }
}

else if(!valid){
    discountPercent = .10;
}

var discountAmount = invoiceSubtotal * discountPercent;
var invoiceTotal = invoiceSubtotal - discountAmount;

$("percent").value = (discountPercent * 100).toFixed(2) ;
$("discount").value = discountAmount.toFixed(2);
$("total").value = invoiceTotal.toFixed(2);

$("type").focus;
}

最佳答案

else if (customerType = 'T')始终求值为true,因为它没有进行任何比较,而是将'T'值分配给customerType。

也许您想做else if (customerType == 'T')吗?

09-12 21:06