在事件中使用参数调用函数 [英] Call function with argument in event

查看:86
本文介绍了在事件中使用参数调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个input,并附加了一个change事件.在该更改事件中,我调用了一个函数.这是代码:

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);

那很好.但是我想传递一个参数给它. myFunction('arg')当我这样做时,该函数立即调用自身,而不是在发生更改事件时调用.

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.

如何使用参数调用change事件的函数?

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

推荐答案

首先,它是onchange.

绑定函数:

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

或者,如果您不想显式使用bind,只需将对函数的调用放在函数onchange调用中:

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');
};

这篇关于在事件中使用参数调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆