javascript - 在 Underscore.js/Backbone.js 模板中使用 IF 语句

标签 javascript jquery backbone.js underscore.js

我收到一个 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/

相关文章:

javascript - 使用RequireJS : should I define inherited resources

javascript - 如何在Angular中管理js文件

javascript - 如何在 IE 中的 &lt;style&gt; 标签中附加 css 属性

javascript - 点击时获取href值

javascript - 即使在 href 重定向后也能 onclick 吗?

JQuery简单搜索

javascript - Backbone.js 路由不会触发页面渲染

javascript - 迭代 js 集合

javascript - 无论查询字符串发生什么变化,都返回上一页

javascript - 获取选定范围内每个单元格的宽度和高度