Javascript 如果变量具有空值,则获取NaN
在这里,我通过ajax和json获得记录。 如果变量有,我就得到值。 但是,如果变量“performance”有空值,则显示Javascript 如果变量具有空值,则获取NaN,javascript,json,ajax,nan,Javascript,Json,Ajax,Nan,在这里,我通过ajax和json获得记录。 如果变量有,我就得到值。 但是,如果变量“performance”有空值,则显示NaN。 我想打印像00.00这样的数值,而不是NaN 可能吗?如果是,那怎么办? 多谢各位 我的代码 function emp_month_perf(){ jQuery.ajax({ url: "<?php echo base_url(); ?>grade_tasks/emp_monthly_perfo
NaN
。
我想打印像00.00
这样的数值,而不是NaN
可能吗?如果是,那怎么办?
多谢各位
我的代码
function emp_month_perf(){
jQuery.ajax({
url: "<?php echo base_url(); ?>grade_tasks/emp_monthly_performance",
data:'',
type:"GET",
dataType: "json",
success:function(data){
var total_month_earn = data.total_earn_point;
var total_month_point = data.total_point;
var performance;
var per_color;
//var radius = "20px";
//alert(total_point);
performance = (((total_month_earn)/(total_month_point))*100).toFixed(2);
if(performance>80)
{
per_color = "#33CF53";
}
else if(performance>=60 && performance<=80)
{
per_color = "#E0C533";
}
else
{
per_color = "#E12827";
}
//document.getElementById("monthperformance").style.borderRadius = radius;
document.getElementById("monthperformance").style.backgroundColor = per_color;
$('#monthperformance').html(performance);
},
error:function (){}
});
}
setInterval(emp_month_perf, 300000);
功能emp\u month\u perf(){
jQuery.ajax({
url:“等级任务/emp每月绩效”,
数据:“”,
键入:“获取”,
数据类型:“json”,
成功:功能(数据){
var total_month_earn=data.total_earn_point;
var总月点数=data.total点数;
var表现;
色差;
//var radius=“20px”;
//警报(总分);
绩效=((总月收入)/(总月分数))*100;
如果(性能>80)
{
per#color=“#33CF53”;
}
否则,如果(性能>=60&&performance使用OR运算符将数字设置为零
var total_month_earn = data.total_earn_point || 0;
var total_month_point = data.total_point || 0;
但是现在你可以得到1/0,也就是无穷大
另一个选项是检查NaN,然后将值设置为零
var performance = (((total_month_earn)/(total_month_point))*100);
var formatted = isNaN(performance) ? "00.00" : performance.toString(2);
使用OR运算符将数字设置为零
var total_month_earn = data.total_earn_point || 0;
var total_month_point = data.total_point || 0;
但是现在你可以得到1/0,也就是无穷大
另一个选项是检查NaN,然后将值设置为零
var performance = (((total_month_earn)/(total_month_point))*100);
var formatted = isNaN(performance) ? "00.00" : performance.toString(2);
修复方法如下:替换
performance = (((total_month_earn)/(total_month_point))*100).toFixed(2);
与
修复方法如下:替换
performance = (((total_month_earn)/(total_month_point))*100).toFixed(2);
与
什么变量是空的?什么变量是空的?谢谢@epascarello:但这对我不起作用。如果变量有空值,它应该按defaulf 00.00打印。我不确定performance
如何有null
值。在第二个选项中,它应该是“NaN”或“###”或“无穷大”,我必须将变量设置为“格式化”在html响应中。对吗?谢谢@epascarello:但这对我不起作用。如果变量有空值,它应该按defaulf 00.00打印。我不确定performance
如何有null
值。在第二个选项中,它应该是“NaN”或“###”或“无穷大”,我必须在html响应中设置变量“formatted”。对吗?