【问题标题】:jquery template value is array or string - how to handlejquery模板值是数组还是字符串——如何处理
【发布时间】:2012-02-01 23:23:55
【问题描述】:

所以我想
如果值是数组,否则只显示值。 我想我可以编写一个函数来执行此操作,但想知道是否有更好的方法来使用 jquery 模板执行此操作?

<script id="template" type="x-jquery-tmpl"> <table>
  <tr>
    <td>${name}</td>
    <td>{{each value}}${$value}<br/>{{/each}}</td>
  </tr> </table>
</script>

<script>
      var data = [
                    {
                        name: "blah",
                        value: ["1", "2", "3"]
                    },
                    {
                        name: "blah blah",
                        value : "abc"
                    }
                ];

$('#template').tmpl(data).appendTo('#target);
    </script>

<div id="target">

</div>

【问题讨论】:

    标签: jquery jquery-templates


    【解决方案1】:

    这样的事情应该可以工作:

    <script id="template" type="x-jquery-tmpl"> <table>
      <tr>
        <td>${name}</td>
        {{if typeof value == 'array'}}
            <td>{{each value}}${$value}<br/>{{/each}}</td>
        {{/if}}
        {{else}}
            <td>${value}<br/></td>
        {{/else}}
      </tr> </table>
    </script>
    

    【讨论】:

      猜你喜欢
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 2015-01-01
      相关资源
      最近更新 更多