javascript - 如何将 'this' 绑定(bind)到单击监听器并使用事件 - es6

标签 javascript ecmascript-6 event-listener es6-class

我有一个多步骤表单,有 4 个框架集。当我按下“下一步”按钮时,每个人都必须进来(当然)

我的 ES6 模块化代码包含如下内容:

class FormController {
  // 1. describe and initiate object
  constructor() {
    this.nextBtn = $(".next");
    this.next_fs;
    .... 

    this.events();
  }

  // EVENTS LISTENER
  events(){
    this.nextBtn.on("click", this.nextClicked.bind(this));
    // other listeners
  }

  nextClicked() {
    this.next_fs = $(this)
      .parent()
      .next(); // this is the next fieldset
    // some actions...
  }
// rest of the code
}

我的问题如下: 我需要在 nextClicked 函数中绑定(bind)“this”,以便能够使用所有变量和方法,例如 this.next_fsthis.saveData()等等...

但是我还需要知道单击了哪个按钮,但我无法知道这一点,因为 this 不再是“此按钮”,而且我无法传递变量(我们称其为“e”) ) 来追踪 e.target。

我的代码有什么问题吗?我知道这是我没有看到的愚蠢的事情。

谢谢!

最佳答案

But I also need to know which button has been clicked, and I cannot know that because "this" is no more "this button", and I cannot pass a variable (let's call it 'e') to trace the e.target

浏览器的事件触发代码会传递该信息。您只需要阅读它即可。

nextClicked(e) {

关于javascript - 如何将 'this' 绑定(bind)到单击监听器并使用事件 - es6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60042847/

相关文章:

未找到 javascript 模块

javascript - span 标签在使用 ajax 的在线名称检查中不起作用

javascript - 有没有理由不在 JavaScript 中用 `var` 替换 `let`?

javascript - 使用 ionic2、Typescript、RollupJs、es2015 模块时出现 `lodash-es` 的 TS 错误

java - 在 Spring 中对多个事件使用 @EventListener 注释

Javascript:传递 'this' 值

javascript - Chrome 框架时间轴上的白条是什么意思?

javascript - 传递元素子项的未记录方法 : as attributes instead of explicit children

java - 窗口监听器不起作用

java - 从字符串模式中提取城市名称的正则表达式