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

标签 meteor

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

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

在我的 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>

对应的js

Template.carpool_event.events({
    'click': function () {
      Session.set("selected_carpool_event", this._id);
    }
  });

Template.carpool_list.events({
    /**
     * Take Event Handler
     */
    'click .takeEvent': function () {
      console.log("Take event:"+Session.get("selected_carpool_event"));
    }
});

最佳答案

你可以试试这个:

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);
      }
    }
});

或者,如果您出于某种其他原因想要两次点击,或者您可以避免捕获第一次点击,您可以直接定位该按钮,this._id 也应该适用于该按钮(您可以分配一个处理程序到同一模板 carpool_event(模板内任意位置的按钮)

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);
       }
    }
});

关于meteor - 如何获取selectedItem并同时处理按钮单击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14527946/

相关文章:

javascript - 如何从 meteor 中的可编辑 div 中选择值而不是使用 textarea.value?

javascript - 在 meteor 中使用 mongodb 将函数存储在数据库中

testing - 用于测试 Meteor 应用程序的 laika、RTD 和 Safety Harness 之间的主要区别是什么?

javascript - 当第三方小部件更改它自己的 react 数据源时,Meteor 避免双重刷新

javascript - Node 和meteor服务器之间的bcrypt无效比较

node.js - 502 Bad Gateway - Ubuntu 16.04.2 LTS 上的 Meteor 和 nginx

meteor - Meteor 0.8.0 是否不再支持例如{{currentUser.emails.0.address}}?

meteor - 如何访问CreateContainer中的原始props?

meteor restivus如何读取多个queryParams

javascript - 在 Meteor 中访问订阅时出现问题