简体   繁体   English

将参数传递给绑定函数

[英]Pass parameter to bound function

I am running into an issue where I want to pass an 'event' parameter to a function which is being called from a JQuery eventListener. 我遇到了一个问题,我想将'event'参数传递给从JQuery eventListener调用的函数。

$('#todoRemove').on('click', this.removeTask(event));

This immediately calls the function when the page is loaded, then does not work when pressing the button which would kick off the event. 加载页面后,此函数立即调用该函数,然后在按下将启动事件的按钮时不起作用。 What can I change to make it so it calls the method in the prototype but passes the event parameter? 我可以进行哪些更改,使其在原型中调用方法但传递事件参数?

    TaskCtrlr.prototype = {
    init: function () {
        this.setupEventHandlers();
    },
    setupEventHandlers: function () {
        $('#addTask').on('click', this.addTask.bind(this));
        $('#todoRemove').on('click', this.removeTask.bind(this));
/*      $('#todoComplete').on('click', this.completeTask.bind(this));
        $('#doneRemove').on('click', this.removeTask.bind(this));*/
    },
    addTask: function () {
        let taskInput = this.view.getTaskInput();

        let newTask;
        if (this.model.tasks.todo.length == 0) {
            newTask = new Task(0, taskInput.title, taskInput.desc, false);
        } else {
            let id = this.model.tasks.todo[this.model.tasks.todo.length - 1].id + 1;
            newTask = new Task(id, taskInput.title, taskInput.desc, false);
        }

        this.model.addTask(newTask);
        this.view.addTodoTask(newTask);
    },
    completeTask: function (event) {
        console.log('wwwwww');
        console.log(event.target.id);
    },
    removeTask: function (event) {
        console.log('eeeeee');
        console.log(event.target.id);
    }
};

EDIT: CURRENT SOLUTION 编辑:当前解决方案

$('#todoRemove').on('click', event, removeTask);

ERROR: 错误:

jQuery.Deferred exception: removeTask is not defined ReferenceError: removeTask is not defined jQuery.Deferred异常:未定义removeTask ReferenceError:未定义removeTask

Why do you want to pass event ? 您为什么要通过event What does it even refer to? 它甚至指的是什么?

The event object is passed by the caller of the event handler, which is jQuery. 事件对象由事件处理程序的调用者jQuery传递。 You should do exactly the same as you do for the other handlers: 您应该执行与其他处理程序完全相同的操作:

$('#todoRemove').on('click', this.removeTask.bind(this));

jQuery will pass the event object to the function without you having to do anything. jQuery会将事件对象传递给函数,而您无需执行任何操作。

This immediately calls the function when the page is loaded 当页面加载时,这立即调用该函数

$('#todoRemove').on('click', this.removeTask(event));

Yes, it will call it because during registering a call back, you are not really registering a callback but calling your function using this code: 是的,它会调用它,因为在注册回调时,您并不是真正在注册回调,而是使用以下代码调用函数:

this.removeTask(event)

Instead you need to do this. 相反,您需要执行此操作。 I am not sure what event is but the 2nd argument you can use to pass something to the callback: 我不确定是什么事件,但是可以使用第二个参数将某些东西传递给回调:

$('#todoRemove').on('click', event, removeTask);

And you can define removeTask like this: 您可以这样定义removeTask

function removeTask( event ) {
  //...
}

Here is an example you can play with to get comfortable: 这是一个让您感到舒适的示例:

function greet( event ) {
  alert( "Hello " + event.data.name );
}
$( "button" ).on( "click", {
  name: "Karl"
}, greet );   

If you do not pass anything, jQuery will still pass the a parameter to you which contains the event info as shown below: 如果您不传递任何内容,jQuery仍将向您传递一个包含事件信息的参数,如下所示:

function greet2( event ) {
  alert( "Hello " + event.target.id );
}
$( "button" ).on( "click", greet2 );

There is more info here . 还有更多的信息在这里

<== Fiddle Me ==> <==摆弄我==>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM