javascript - 为什么我的三元 if 语句不评估为 NULL?
问题描述
我正在尝试根据通过Ajax
函数从数据库返回的值更改附加按钮的文本。
.append($('<td>').attr('id', "tdBookingStatus" + i).html(val.HasCustomerArrived === true ? "Checked in" : (val.HasCustomerArrived == null) ? " ": "Cancelled"))
但它对 NULL 不起作用,即使函数返回 NULL 但它不起作用我试过了,==
但没有任何作用。===
!
解决方案
您还需要检查=== ''
您是否正在考虑该值。使用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);
推荐阅读
- oracle - 如何使用 PL/SQL 代码在 Apex Oracle 中显示十进制值
- python - pip 在安装兼容的包版本时是否考虑到实际的 Python 版本?
- android - 在 Espresso 中模拟对象
- android - 删除 kotlin android 中浮点值的科学记数法格式
- java - getFramesAtIndex() 中的 MediaMetadataRetriever.BitmapParams 参数是什么?
- php - PHP邮件功能错误客户端未通过MAIL FROM期间发送匿名邮件的身份验证
- excel - 导出跨越两列的一系列单元格的有效方法
- php - 如何将我的 Window 页面从名为 php 的 ajax 函数重定向到另一个 URL,我不想从 jQuery 或 javascript 重定向它
- bash - bash 源文件中的多个变量定义行为不同
- azure - 由于在 Azure CosmosDB 中的物理分区上存在时间,文档存档有什么影响