JavaScript 比较和逻辑运算符

时间:2021-03-30 13:25:29

标签: javascript

如果两个值是 NULL"",我正在尝试使用 js 将 css 样式添加到我的 html。我有以下代码:

if (v.doctor_id === null || v.doctor_id === "") {
    v.doctor_id = 0;
}   
if (v.assistant_id === null || v.assistant_id === "") {
    v.assistant_id = 0;
}
...
if (v.doctor_id = 0 && v.assistant_id = 0) {
    html += '<td class="ci6 text-right" style="background-color: green;">' + $('.price').masked(v.app_price) + '</td>';
} else {
    html += '<td class="ci6 text-right" style="background-color: red;">' + $('.price').masked(v.app_price) + '</td>';
}

我的两个值是 doctor_idaasistant_id。我正在检查两者是否为空或“”。

现在我总是得到红色的背景颜色。

1 个答案:

答案 0 :(得分:2)

问题就在这里:

if (v.doctor_id = 0 && v.assistant_id = 0) { 

您使用了错误的运算符使用 =====

if (v.doctor_id === 0 && v.assistant_id === 0) {
        html += '<td class="ci6 text-right" style="background-color: green;">' + $('.price').masked(v.app_price) + '</td>';
    } else {
        html += '<td class="ci6 text-right" style="background-color: red;">' + $('.price').masked(v.app_price) + '</td>';
    }