【问题标题】:Meteor template.find is undefinedMeteor template.find 未定义
【发布时间】:2012-12-05 13:18:27
【问题描述】:

我正在尝试使用template.find 让我的生活更轻松。

但在 javascript 控制台中我得到:undefined is not a function

这就是我所拥有的。它在template.find(...) 上被绊倒了

  Template.superuserHUD.events = 
  {
    'click input.new_device': function (template) {
      var id = template.find(".new_device_id").value;
      Device_IPs.insert({ID: id, IP: "Not Connected"});
    }
  }

有什么想法吗?

【问题讨论】:

  • 你应该使用.getElementById()方法直接检索元素的值文本,而不是找到匹配选择器的元素
  • 您好 user1191551,您已提出 5 个问题,但未接受任何答案。接受您认为有用的答案被认为是一种普遍的礼貌。它还将提高对您未来问题的回答质量。谢谢!
  • 感谢拉胡尔。看来我似乎有 2 个不同的帐户,因为我肯定回答并接受了比此帐户显示的更多的答案...

标签: javascript events templates meteor


【解决方案1】:

事件处理函数接收两个参数:event,一个包含事件信息的对象,以及template,一个模板实例,用于定义处理程序的模板。

第二个参数是可选的,但是当你想使用find()findAll()firstNode()lastNode()等模板实例函数时,需要在handler中接收。

因此,要在事件处理程序中使用 template.find(),您需要将两个参数都传递为:

'click input.new_device': function (event, template) {
     // your event handling code
  }

【讨论】:

  • 嗯。我试过这个:'click input.new_device': function (event, template) { var id = this.find(".new_device_id").value; console.log(id) } 但它仍然没有工作。仍然未定义...这是html:ID:
  • 你仍然做错了,在'click input.new_device' this 将引用当前的window 对象,不是你正在尝试工作的template和。所以,显然this.find() 将返回undefined。所以,你必须写template.find()而不是this.find()
【解决方案2】:

请使用第二个参数

Template.superuserHUD.events
    'click input.new_device': (event, template) ->
        options =
              id: template.find('.new_device_id').value
              IP: 'Not Connected'
        Device_IPs.insert options

有时像使用模板本身一样

Template.superuserHUD.events
    event: (event, template) ->
        @find('.new_device_id').value

对于咖啡文盲,在 javascript 中也是如此......

Template.superuserHUD.events({
  'click input.new_device': function(event, template) {
    var options;
    options = {
      id: template.find('.new_device_id').value,
      IP: 'Not Connected'
    };
    return Device_IPs.insert(options);
  }
});

Template.superuserHUD.events({
  event: function(event, template) {
    return this.find('.new_device_id').value;
  }
});

【讨论】:

  • 对不起,我完全不明白这个语法。这是javascript吗?我从未见过“->”
猜你喜欢
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 2015-11-06
  • 2016-07-21
  • 2017-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多