【发布时间】:2020-02-03 16:00:24
【问题描述】:
我有一个多步骤表单,有 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 函数中绑定“this”,以便能够使用所有变量和方法,如this.next_fs、this.saveData() 等...
但是我还需要知道哪个按钮被点击了,我无法知道,因为this 不再是“这个按钮”,我不能传递一个变量(我们称之为'e')来跟踪e .target.
我的代码有什么问题?我知道那是我没有看到的愚蠢的东西。
谢谢!
【问题讨论】:
-
嗯...部分。之后,我必须使用自变量在构造函数中存储“this”引用。我会尝试,但我相信某处有更聪明的解决方案......
标签: javascript ecmascript-6 event-listener es6-class