为什么我的三元if语句不能评估为NULL?

问题描述 投票:1回答:1

我试图根据数据库通过Ajax函数返回的值更改附加按钮的文本。

 .append($('<td>').attr('id', "tdBookingStatus" + i).html(val.HasCustomerArrived === true ? "Checked in" : (val.HasCustomerArrived == null) ? " ": "Cancelled"))

但它不适用于NULL,即使函数返回NULL但它不起作用我尝试=====!但没有任何作用。

javascript jquery html ternary-operator
1个回答
0
投票

如果您正在考虑该值,还需要检查=== ''。使用null无效。

//for blank value
var test = '';
var res = test === true ? "Checked in" : (test === null || test === '') ? " ": "Cancelled"

console.log(res);

//for null value
var test = null;
var res = test === true ? "Checked in" : (test === null || test === '') ? " ": "Cancelled"

console.log(res);

//for true value
var test = true;
var res = test === true ? "Checked in" : (test === null || test === '') ? " ": "Cancelled"
console.log(res);

//for false value
var test = false;
var res = test === true ? "Checked in" : (test === null || test === '') ? " ": "Cancelled"
console.log(res);
© www.soinside.com 2019 - 2024. All rights reserved.