Jquery 模板 - else if 构造

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4989589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:20:01  来源:igfitidea点击:

Jquery template - else if construct

jquerytemplatesif-statement

提问by L G

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?

我试图在 jquery 模板块中使用“else if”语句。我在其他示例中看到,不是编写 if else,我们可以只放置 else。但这对我不起作用,它在模板。有人可以帮忙吗?

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.

我在下面提到了我为了实现这一点而编写的 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>

回答by The_Butcher

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

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

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

回答by Adrien Be

I had a similar issue when performing an if statement inside a loop, when doing the if statement on an item of the loop.

我在循环中执行 if 语句时遇到了类似的问题,在循环的一个项目上执行 if 语句时。

Assuming you are having an issue for the same reason (I see that your question includes some tdand trso you're probably iterating through items and displaying the their content in a table. Then here is my answer:

假设您出于同样的原因遇到问题(我看到您的问题包括一些问题tdtr因此您可能正在遍历项目并将其内容显示在表格中。那么这是我的答案:

<script type="text/x-jquery-tmpl" id="jqTemplate">
 <ul>
      {{each data.messagetext}}
         <li>
             Message # ${$index+1}: &quot;${$value}&quot; |
             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 $value}},这是常规的技巧{{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}}

如果你想检查你正在迭代的项目的字段,你可以做 {{if $value.fieldname}}exists{{else}}does not exist{{/if}}

fiddle http://jsfiddle.net/x2Tac/

小提琴http://jsfiddle.net/x2Tac/

jquery-template doc http://www.jquerysdk.com/api/template-tag-if

jquery-template 文档http://www.jquerysdk.com/api/template-tag-if

回答by Michael

The correct answer is:

正确答案是:

{{else [Condition]}}

{{else [条件]}}

Make sure that you import the jquery template js.

确保您导入了 jquery 模板 js。