简体   繁体   English

如何将函数参数传递给 JavaScript 中的回调函数?

[英]How to pass function parameters to callback function in JavaScript?

I have created an OnClick handler function as below:我创建了一个 OnClick 处理函数,如下所示:

function onClickHandler(ID, doClick) {
  $('#' + ID).on('click', doClick);
}

I have created another function that will redirect after clicking a button:我创建了另一个在单击按钮后将重定向的函数:

function redirectPage(url) {
    window.location.href = url;
}

If I'm passing the url parameters as below, then I get only events of clicked button:如果我传递如下 url 参数,那么我只会得到单击按钮的事件:

onClickHandler(CreateLODControlIds.BTN_CANCEL, redirectPage.bind(LODPage.DASHBOARD));

How can I pass the url parameters of redirectPage function in onClickHandler function?如何在onClickHandler函数中传递redirectPage函数的url参数?

Any help is much appreciated!任何帮助深表感谢!

The first argument to bind is the this value. bind第一个参数是this值。

The value you want to bind to the url parameter needs to be passed as the second argument to bind .要绑定到url参数的值需要作为第二个参数传递给bind

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

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