为什么使用&&的IF条件不能在javascript中工作?

时间:2018-12-12 04:37:16

标签: javascript html

我在使用JavaScript中的&&条件时遇到问题。条件语句不起作用,我也不知道为什么。这里的代码:

<script type = 'text/javascript'>
function computeLoan(){
var leasing = document.getElementById('leasing').value; // the data are taken from select option in HTML
var tenor = parseInt(document.getElementById('tenor').value); // the data are taken from select option in HTML
var ten = tenor/2;
var otr = document.getElementById('otr').value;
var dp = document.getElementById('dp').value;
// var total = 0;

if ((leasing = "PT. Adira Dinamika Multi Finance, Tbk") && (tenor == "24")) 
{
    interest = 0.039;
}    
if ((leasing = "PT. Adira Dinamika Multi Finance, Tbk") && (tenor == "36")) 
{
    interest = 0.042;
}
if ((leasing == "PT. Wahana Ottomitra Multiartha (WOM)") && (tenor == '24')) 
{
    interest = 0.036;
}
if ((leasing == "PT. Wahana Ottomitra Multiartha (WOM)") && (tenor == '36')) 
{
    interest = 0.041;
}
if ((leasing == "PT. Summit Oto Finance") && (tenor == '24')) {
    interest = 0.0355;
}
if ((leasing == "PT. Summit Oto Finance") && (tenor == '36')) {
    interest = 0.04;
}

total = (((otr - dp) * interest * ten) + (otr - dp))/tenor;
$("#monthly").val(total);

}
</script> 

1 个答案:

答案 0 :(得分:1)

问题是第一个if语句:

if ((leasing = "PT. Adira Dinamika Multi Finance, Tbk") && (tenor == "24")) 

上面写着

leasing = "PT. Adira Dinamika Multi Finance, Tbk"

您使用了错误的=符号-应该为==。 =是变量赋值运算符,==是等价运算符。修复if语句,使其看起来像这样:

if ((leasing == "PT. Adira Dinamika Multi Finance, Tbk") && (tenor == "24")) 

您的代码将起作用。