Meteor 如何获取selectedItem并同时处理按钮单击事件?

Meteor 如何获取selectedItem并同时处理按钮单击事件?,meteor,Meteor,与其先选择一个项目,然后使用按钮对所选项目执行某些操作,我更希望用户执行一次单击 这似乎有效,但我担心按钮上的事件必须发生在拼车事件之后。这似乎不对。有没有更好的方法来处理这个问题?谢谢 在我的HTML中 <template name="carpool_list"> <h1>Carpool</h1> {{#each carpool_events}} <ul> {{> carpool_event}} </ul>

与其先选择一个项目,然后使用按钮对所选项目执行某些操作,我更希望用户执行一次单击

这似乎有效,但我担心按钮上的事件必须发生在拼车事件之后。这似乎不对。有没有更好的方法来处理这个问题?谢谢

在我的HTML中

<template name="carpool_list">
  <h1>Carpool</h1>
  {{#each carpool_events}}
  <ul>
    {{> carpool_event}}
  </ul>
  {{else}}
    No events yet.
  {{/each}}
</template>

<template name="carpool_event">
  <div class="carpool_event">
    <span class="localDate">{{localDate}}</span>
    <span class="owner">{{owner}}</span>
    {{#if currentUser}}
      <span><input type="button" class="takeEvent" value="Take Event"/></span>
    {{/if}}
  </div>
</template>
你可以试试这个:

Template.carpool_event.events({
"click": function () {
      Session.set("selected_carpool_event", this._id);
      if($(event.target).attr("class") == "takeEvent" && Meteor.userId) {
         console.log("Take event:"+this._id);
      }
    }
});
或者,如果您出于某些其他原因想要两次单击,或者您可以避免捕获第一次单击,您可以直接将按钮作为目标,
这一点。_id
也应该对按钮起作用(您可以为模板内任意位置的按钮为同一模板
拼车事件
指定处理程序)


为什么不使用一键式处理程序?是否在某处使用#each?你能详细说明一下你是如何使用它的吗?您可以在按钮单击中使用_id而不是请求会话。获取变量如果在按钮上仅使用一次单击处理程序,如何获取_id?当我看到目标或currentTarget时,它只是一个按钮。是的,拼车活动模板在另一个模板中。我会尽快发布代码。谢谢,谢谢!我会尝试一下,然后再报告。
Template.carpool_event.events({
"click": function () {
      Session.set("selected_carpool_event", this._id);
      if($(event.target).attr("class") == "takeEvent" && Meteor.userId) {
         console.log("Take event:"+this._id);
      }
    }
});
Template.carpool_event.events({
    /**
     * Take Event Handler
     */
    "click .takeEvent": function () {
       Session.set("selected_carpool_event", this._id);
       if(Meteor.userId) {
           console.log("Take event:"+this._id);
       }
    }
});