【发布时间】:2012-07-12 13:58:01
【问题描述】:
我收到一个 JSON 对象,其中一个值为 null。 JSON 看起来像:
[{"id":"1096","price":null,
现在,它正在使用以下代码将NULL 字符串输出到网页。 (我在 Backbone.js/Underscore.js 中使用模板引擎)
<div class="subtitle">$<%= price %></div>
因为如果没有返回price,我想隐藏整个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 语句有关
【问题讨论】:
-
您确定要从数组中传入单个对象吗?我们可以查看您的整个下划线模板以及您传入的值吗?
标签: javascript jquery backbone.js underscore.js