javascript - Meteor - 如果标记嵌套在 {{#with}} block 中,则 DOM 查询不起作用

标签 javascript jquery meteor

HTML

<template name="formExample">
  <form>
  {{#with formData}}

    <input type="text" class="example">

  {{/with}}
  </form>
</template>

JS

Template.formExample.helpers({
  'formData': function() {
    return Settings.findOne({ collection_id : getCollectionId() })
  }
})


Template.formExample.rendered = function(){

  // Does not work
  console.log( $('.example'), document.querySelectorAll('.example') )
  // >> undefined []

  // Works (but obviously bad)
  setTimeout(function() {
    console.log( $('.example') )
  }, 1000)

  // Does not work
  console.log( this.$('.example'), this.find('.example') )

  // >> [prevObject: jQuery.fn.init[0], context: li] null
}

如何使用逻辑 block 内的代码有效查询 DOM、init 函数等?我希望 template.$template.find 方法能够工作。

最佳答案

你在哪里设置formData? 我尝试了你的例子,它似乎有效。

测试:http://meteorpad.com/pad/tXKpZ6gxpGur5pDds

关于javascript - Meteor - 如果标记嵌套在 {{#with}} block 中,则 DOM 查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31888108/

相关文章:

javascript - 如何让一个<div>上的 'setTimeout'自动转到另一个<div>?

javascript - 使用 chagePage 手动传递额外值

meteor - 如何在 meteor 中包含来自CDN的JavaScript?

javascript - 向meteor方法提交表单值但出现错误userSchema not Defined

javascript - 使用键从集合动态创建树对象

javascript - 滚动过去时激活菜单类

javascript - 允许 MVC 将 Controller 操作响应视为静态内容

javascript - 获取具有特定 data-* 集的元素

javascript - 如何在使用 AOS 向上滚动时触发动画

xcode - clang 前端命令失败,退出代码为 70(使用 -v 查看调用)