Call function with argument in event

你离开我真会死。 提交于 2019-12-14 03:13:14

问题


I have an input, and attached a change event to it. In that change event I called a function. Here's the code:

textInput.change(myFunction);

That works fine. But I want to pass a parameter to it. myFunction('arg') When I do that, the function calls itself right away, and not when there's a change event.

How can I call a function to the change event with arguments?


回答1:


First, it's onchange.

Either bind the function:

textInput.onchange = myFunction.bind(this, 'a');

Or, if you don't want to explicitly use bind, just put the call to the function in the function onchange calls:

textInput.onchange = function () {
  myFunction('a');
};



回答2:


textInput.change(myFunction.bind(textInput, 'arg'));

function myFunction(arg1, event) {}



回答3:


I would just pass an anonymous function like this:

textInput.change(function(){
  myFunction('arg');
});


来源:https://stackoverflow.com/questions/34143802/call-function-with-argument-in-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!