我收到一个 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
语句有关
最佳答案
呃,你不想吗(没有感叹号)
<% if (price) { %>
<div class="subtitle">$<%= price %></div>
<% } %>
因为您当前说如果没有价格则显示价格...这是没有意义的。
关于javascript - 在 Underscore.js/Backbone.js 模板中使用 IF 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11453458/