我有一个多步骤表单,有 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_fs
、this.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/