【问题标题】:how to save value from HTML id for comparing in for loop如何从 HTML id 中保存值以在 for 循环中进行比较
【发布时间】:2015-08-26 19:04:33
【问题描述】:

我想在 for 循环中比较两个值,已经尝试使用 #tmpid.val ,但不起作用。

<template name="productpendingstatus">

       {{#each totaltemplate}}

       <table class="table table-responsive table-bordered">

        <thead><tr><td colspan="3">{{this.META.TEMPLATE_NAME}}</td>

<td id="tmpid">{{this._id}}</td> <want to compare this value in code below>

</tr></thead>
        </table>
        <div class="pendingProducts">
          <table class="table table-responsive table-bordered">

            <tbody>


                {{#each productt}}
                {{#if $eq <value of id tmpid> this.TemplateID.value }}//this is ques

                <tr>
                  <td><input type="checkbox"></td>
                  <td>{{this.ProductId.value}}</td>

                </tr> 
                {{/if}}
                {{/each}}


            </tbody>

          </table>
       </div>
       {{/each}}



    </template>

【问题讨论】:

  • 您使用的是哪个模板框架?请将其作为标签添加到您的问题中。
  • 您的 id="tmpid" 正在使用您的循环多次渲染。静态的id 没有降神。可能有一个或多个模板在 HTML 页面上多次创建相同的 ID
  • 是的,我会在每个循环中进行比较,您也可以将其更改为动态,没问题
  • @OhAuth 没听懂你..你要求用于比较的包吗?
  • @garima 这个问题被标记为 HTML,但它不是直接的 HTML,实际上并没有质疑 HTML。请标记使用您的示例中看到的花括号块的框架。它将帮助了解框架的用户找到您的问题。

标签: html meteor


【解决方案1】:

为了在模板中引用父数据上下文,空格键现在提供the "../" notation

<template name="productpendingstatus">
       {{#each totaltemplate}}
       <table class="table table-responsive table-bordered">
        <thead><tr><td colspan="3">{{this.META.TEMPLATE_NAME}}</td>
<td id="tmpid">{{this._id}}</td> <want to compare this value in code below>
</tr></thead>
        </table>
        <div class="pendingProducts">
          <table class="table table-responsive table-bordered">
            <tbody>
                {{#each productt}}
                {{#if $eq ../_id this.TemplateID.value }}
                <tr>
                  <td><input type="checkbox"></td>
                  <td>{{this.ProductId.value}}</td>
                </tr> 
                {{/if}}
                {{/each}}
            </tbody>
          </table>
       </div>
       {{/each}}
    </template>

【讨论】:

    【解决方案2】:

    您可以使用助手来获取父数据

    <template name="productpendingstatus">
    {{#each totaltemplate}}
    <table class="table table-responsive table-bordered">
      <thead>
        <tr><td colspan="3">{{this.META.TEMPLATE_NAME}}</td>
        <td id="tmpid">{{this._id}}</td> 
        </tr></thead>
    </table>
    <div class="pendingProducts">
      <table class="table table-responsive table-bordered">
        <tbody>
          {{#each productt}}
          {{#if isPending }}
          <tr>
            <td><input type="checkbox"></td>
            <td>{{this.ProductId.value}}</td>
          </tr> 
          {{/if}}
          {{/each}}
        </tbody>
      </table>
    </div>
    {{/each}}
    

    然后在js文件上

     Template.productpendingstatus.helpers({
      isPending: function(){
        var parentData = Template.parentData();
        return parentData._id === this.TemplateID.value;
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2017-09-17
      • 2021-09-19
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 2018-03-28
      • 1970-01-01
      相关资源
      最近更新 更多