首页 > 解决方案 > 如果 Jquery 中的值为 null 或为空,则分配 0

问题描述

我想为下面的 jQuery 语句使用三元运算符,例如如果 employee_salary 是emptyor null,我想分配为 0(零)。否则,只需分配实际值。

jQuery('#employee_form #employee_salary').val(parseInt(selected_table_data['employee_salary']))

标签: javascriptjqueryternary

解决方案


您可以ternary简单地使用运算符。

selected_table_data['employee_salary']
  ? parseInt(selected_table_data['employee_salary'])
  : 0

console.log('' || 0);
console.log(null || 0);
console.log(undefined || 0);


推荐阅读