Jquery 模板 - else if 构造

发布于 2024-10-17 04:33:02 字数 737 浏览 0 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十年九夏 2024-10-24 04:33:02

根据 JQuery API,语法是正确的。也许尝试删除 '(' 括号?

{{if Amount == ""}}
   -
{{else balance == ""}}
   *                      
{{else}}
   ${Amount}
{{/if}}

The syntax is correct as per JQuery API. Maybe try removing the '(' parentheses?

{{if Amount == ""}}
   -
{{else balance == ""}}
   *                      
{{else}}
   ${Amount}
{{/if}}
笑着哭最痛 2024-10-24 04:33:02

在循环内执行 if 语句、对循环的某个项目执行 if 语句时,我遇到了类似的问题。

假设您出于同样的原因遇到问题(我发现您的问题包括一些 td 和 tr,因此您可能会迭代项目并在那么这是我的答案:

<script type="text/x-jquery-tmpl" id="jqTemplate">
 <ul>
      {{each data.messagetext}}
         <li>
             Message # ${$index+1}: "${$value}" |
             Test result: {{if $value}}exists{{else}}does not exist{{/if}}
         </li> 
      {{/each}}
    </ul>
</script>

注意 {{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 and tr so you're probably iterating through items and displaying the their content in a table. Then here is my answer:

<script type="text/x-jquery-tmpl" id="jqTemplate">
 <ul>
      {{each data.messagetext}}
         <li>
             Message # ${$index+1}: "${$value}" |
             Test result: {{if $value}}exists{{else}}does not exist{{/if}}
         </li> 
      {{/each}}
    </ul>
</script>

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

橘味果▽酱 2024-10-24 04:33:02

正确答案是:

{{else [Condition]}}

确保导入了 jquery 模板 js。

The correct answer is:

{{else [Condition]}}

Make sure that you import the jquery template js.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文