Jquery 模板 - else if 构造
我试图在 jquery 模板块中使用“else if”语句。我在其他示例中看到,我们可以不写 if else,而只写 else。但这对我不起作用,它会在模板。有人可以帮忙吗?
它的实际工作方式就像
if(Amount == "")
{
-
}
else if(balance == "")
{
*
}
else
{
Amount
}
我在下面提到的 jquery 模板语法一样,我编写它是为了实现这一点。
<script id="MyTemplate" type="text/x-jquery-tmpl">
<tr style="background-color:#EFF3FB"><td onclick="test();">${CardNumber}</td><td>${PostDate}</td>
<td>{{if (Amount == "")}}-
{{else(balance == "")}}*
{{else}}${Amount}
{{/if}}
</td>
<td>${Description}</td></tr>
</script>
I was trying to use an "else if" statement inside the jquery template block.I have seen in other samples that instead of writing if else, we can just put else.But this is not working for me, its throwing syntax error on the template.can any one help pls?
The acutual way it should work is like
if(Amount == "")
{
-
}
else if(balance == "")
{
*
}
else
{
Amount
}
I have mentioned below the jquery template syntax which i wrote to acheive this.
<script id="MyTemplate" type="text/x-jquery-tmpl">
<tr style="background-color:#EFF3FB"><td onclick="test();">${CardNumber}</td><td>${PostDate}</td>
<td>{{if (Amount == "")}}-
{{else(balance == "")}}*
{{else}}${Amount}
{{/if}}
</td>
<td>${Description}</td></tr>
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 JQuery API,语法是正确的。也许尝试删除
'('
括号?The syntax is correct as per JQuery API. Maybe try removing the
'('
parentheses?在循环内执行 if 语句、对循环的某个项目执行 if 语句时,我遇到了类似的问题。
假设您出于同样的原因遇到问题(我发现您的问题包括一些 td 和 tr,因此您可能会迭代项目并在那么这是我的答案:
注意
{{if $value}}
中的美元符号,这是常规{{if value}}
的技巧,如果你想要检查您正在迭代的项目的字段,可以执行
{{if $value.fieldname}}exists{{else}}does not exit{{/if}}
fiddle http://jsfiddle.net/x2Tac/
jquery-template 文档 http://www.jquerysdk.com/api/template-tag-if
I had a similar issue when performing an if statement inside a loop, when doing the if statement on an item of the loop.
Assuming you are having an issue for the same reason (I see that your question includes some
td
andtr
so you're probably iterating through items and displaying the their content in a table. Then here is my answer:notice the dollar sign in
{{if $value}}
, this is the trick on the regular{{if value}}
if you want to check the field of an item you are iterating on you can do
{{if $value.fieldname}}exists{{else}}does not exist{{/if}}
fiddle http://jsfiddle.net/x2Tac/
jquery-template doc http://www.jquerysdk.com/api/template-tag-if
正确答案是:
{{else [Condition]}}
确保导入了 jquery 模板 js。
The correct answer is:
{{else [Condition]}}
Make sure that you import the jquery template js.