使用Undercore.js/Backbone.js模板中的IF语句
Using IF statements in Underscore.js/Backbone.js templates
我收到一个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>
<% } %>
因为你现在说如果没有价格,那么显示价格。。。这毫无意义。
null
不是undefined
!
如果json对象被正确解码,那么检查(price)
或(price != null)
应该没问题。
它不应该是比较的==
(在这种情况下是!==
)吗
<% if (price !== null) { %>
<div class="subtitle">$<%= price %></div>
<% } %>
例如,查看Jsbin.com的行的警告
相关文章:
- 下划线.js if 语句上的字符串连接
- JS if 语句和单选按钮
- 按 ID 的 JS IF 语句
- 正在添加项目id's到js-if语句
- knockout.js:If和foreach绑定,检查可观察数组上的真值
- 简单JS if语句不起作用
- 我的JS if语句有问题吗
- 在JS“if”语句参数中,是否有用于声明本地作用域变量的简明语法
- 车把中的逻辑运算符.js {{#if}} 条件
- JS if/else速记的快速翻译
- Node.js: if-modified-since date总是不同于mtime stat文件
- JS if/else trouble
- Hogan JS IF statements
- 为什么这个js if子句没有停止循环
- 使用JS 'if'语句来检测图像是否被拖到HTML5画布上的正确位置
- d3.js if else正则表达式字符串匹配以更改样式
- JS if语句没有从json响应中计算
- js if条件的问题
- OnClick JS If Statement
- 这个JS if条件测试有什么问题