javascript - 如果 Jquery 中的值为 null 或为空,则分配 0
问题描述
我想为下面的 jQuery 语句使用三元运算符,例如如果 employee_salary 是empty
or null
,我想分配为 0(零)。否则,只需分配实际值。
jQuery('#employee_form #employee_salary').val(parseInt(selected_table_data['employee_salary']))
解决方案
您可以ternary
简单地使用运算符。
selected_table_data['employee_salary']
? parseInt(selected_table_data['employee_salary'])
: 0
console.log('' || 0);
console.log(null || 0);
console.log(undefined || 0);
推荐阅读
- authentication - 无法将 id_token 与 python 的 REQUESTS 库一起使用 webapi
- keras - keras 拟合函数中的尺寸误差(keras 中的条件变分自动编码器)
- java - 如何在 Java 应用程序中将 Apache Tomcat Embed 从 9.0.35 更新到 10.0.4
- python - WinError 3 系统找不到指定的路径:'Desktop\\sem_8_project\\brain\\brain_tumor_dataset'
- postgresql - 文本表达式的 SQLAlchemy 反射不支持自动生成跳过功能索引
- python - 使用python的潜在因子建模和高斯过程
- xsd - 这个 XSD 限制是什么意思。具有限制的派生类型只是基类型的一个属性
- mysql - 错误地停止在本地主机上运行后无法启动 MySQL 服务器
- avro - confluent_kafka.error.ValueSerializationError: KafkaError{code=_VALUE_SERIALIZATION,val=-161 : ValueError
- javascript - 有没有办法让 react-compound-timer 在达到一个值时自动重置而不按任何按钮?