问题
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