我收到了一个 JSON 对象,其中一个值是 null
。JSON 如下所示:
[{"id":"1096","price":null,
现在,使用以下代码将NULL
字符串输出到网页。 (我正在使用Backbone.js / Underscore.js中的模板引擎)
<div class="subtitle">$<%= price %></div>
因为我希望在没有返回价格时隐藏整个 div
,所以我添加了 if
语句:
<% if (price) { %>
<div class="subtitle">$<%= price %></div>
<% } %>
然而,它似乎仍然输出了
div.subtitle
。我做错了什么?我也尝试了以下方法,但它们没有起作用。<% if (typeof(price) != "undefined") { %>
<div class="subtitle">$<%= price %></div>
<% } %>
<% if (price != null) { %>
<div class="subtitle">$<%= price %></div>
<% } %>
<% if (price != "null") { %>
<div class="subtitle">$<%= price %></div>
<% } %>
我怀疑这与在Underscore.js的模板中使用if
语句有关