Jquery 如果json返回null,则设置“0”
我在一个div中显示json数据,它运行良好Jquery 如果json返回null,则设置“0”,jquery,ternary-operator,Jquery,Ternary Operator,我在一个div中显示json数据,它运行良好 var details= response.data.filters; $.each(details,function(i,value){ $('.subSection').append('<section><label>Speed</label><label>'+value.speed+'</label></section>'); }); 但是它连续返回零,就
var details= response.data.filters;
$.each(details,function(i,value){
$('.subSection').append('<section><label>Speed</label><label>'+value.speed+'</label></section>');
});
但是它连续返回零,就像这样..0000
。这不是一个正确的结构化元素
我想我在三元运算符中做错了什么。如果我做错了,有人能纠正我吗?问题是您的操作顺序,您需要将整个三元运算符包装在括号中。您的声明有效地评估为:
$(".subSection").append(
("<section><label>Speed</label><label>" + (value.speed == null))
? "0"
: (value.speed + "</label></section>")
);
问题在于你的操作顺序,你需要把你的整个三元组包装在帕伦中。您的声明有效地评估为:
$(".subSection").append(
("<section><label>Speed</label><label>" + (value.speed == null))
? "0"
: (value.speed + "</label></section>")
);
尝试用括号括起来,如
$('.subSection').append('<section><label>Speed</label><label>'+((value.speed==null)?'0':value.speed)+'</label></section>');
$('.subSection').append('Speed'+((value.Speed==null)?'0':value.Speed)+';
希望这有帮助 尝试用括号括起来,如
$('.subSection').append('<section><label>Speed</label><label>'+((value.speed==null)?'0':value.speed)+'</label></section>');
$('.subSection').append('Speed'+((value.Speed==null)?'0':value.Speed)+';
希望这有帮助
$('.subSection').append('<section><label>Speed</label><label>'+((value.speed==null)?'0':value.speed)+'</label></section>');