首页 > 解决方案 > 为什么我的三元 if 语句不评估为 NULL?

问题描述

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

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

但它对 NULL 不起作用,即使函数返回 NULL 但它不起作用我试过了,==但没有任何作用。===!

标签: javascriptjqueryhtmlternary-operator

解决方案


您还需要检查=== ''您是否正在考虑该值。使用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);


推荐阅读