将参数传递给 javascript 回调函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8359048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:11:39  来源:igfitidea点击:

Passing parameters to a javascript callback function

javascript

提问by JIbber4568

I've been trying to work out how to pass additional parameters to a javascript callback function.

我一直在研究如何将附加参数传递给 javascript 回调函数。

In similar posts users have succeeded using anonymous function (these are new to me so I may have been doing them wrong) but I just can't get them to fire.

在类似的帖子中,用户成功地使用了匿名功能(这些对我来说是新的,所以我可能做错了),但我无法让他们开火。

The below is what I have now, I need to be able to pass the itemId to the function "ajaxCheck_Callback" as well as the response.

下面是我现在所拥有的,我需要能够将 itemId 传递给函数“ajaxCheck_Callback”以及响应。

Hope this makes sense.

希望这是有道理的。

Thanks for the help.

谢谢您的帮助。

function delete(itemId)
{
  selectpage.ajaxCheck(itemId, ajaxCheck_Callback);
}


Function ajaxCheck_Callback(response)

{
  alert(response);
  alert(itemId);
}

Thanks for the help guys. I now get undefined on my alert whereas previously this was alerting correctly.

谢谢你们的帮助。我现在在我的警报中未定义,而以前这是正确警报。

function delete(itemId)
{
  selectpage.ajaxCheck(itemid, function () {ajaxCheck_Callback(itemid); });
}


function ajaxCheck_Callback(response)

{
  alert(response);
  alert(itemId);
}

回答by Alex Turpin

The way is usually done is to use an anonymous function.

通常的做法是使用匿名函数。

function deleteItem(itemId) {
    selectpage.ajaxCheck(itemId, function() { ajaxcheck_Callback(itemId); });
}

function ajaxCheck_Callback(response)
{
    alert(response);
}

Careful with your syntax, there are some errors in there. The functionkeyword has a lower-case fand you can't have a function named delete, that's a reserved keyword.

小心你的语法,那里有一些错误。该function关键字具有较低的情况下f,你不能有一个名为功能delete,这是一个保留关键字。

回答by Adam Rackis

Create an anonymous function, which calls your callback, and supplies the needed parameter.

创建一个匿名函数,它调用您的回调并提供所需的参数。

function deleteItemById(itemId)
{
  selectpage.ajaxCheck(itemId, function() { ajaxCheck_Callback(itemId); });
}

The anonymous function will create a closure over itemId, and therefore will still have access to this value even after the original call to delete has long since ended.

匿名函数将在 上创建一个闭包itemId,因此即使在对 delete 的原始调用早已结束之后仍然可以访问该值。

For completeness, also note that deleteis a reserved word in JavaScript; you need to name this function something else.

为了完整起见,还要注意这delete是 JavaScript 中的保留字;您需要将此函数命名为其他名称。

回答by Matt Ball

As your question mentions, you can do it using an anonymous function:

正如您的问题所提到的,您可以使用匿名函数来做到这一点:

function deleteItem(itemId)
{
  selectpage.ajaxCheck(itemId, function ()
  {
      ajaxCheck_Callback(itemId);
  });
}

// Note, lowercase 'f' in 'function'
function ajaxCheck_Callback(response)
{
  alert(response);
}

It can also be done using a named function, which you might find more readable:

也可以使用命名函数来完成,您可能会发现它更具可读性:

function deleteItem(itemId)
{
    function onAjaxCheck()
    {
        ajaxCheck_Callback(itemId);
    }

  selectpage.ajaxCheck(itemId, onAjaxCheck);
}


N.B.as @Xeon06 points out, deleteis a reserved word, so that is not a valid function name.

NB为@ Xeon06指出的delete是一个保留字,所以这不是一个有效的函数名。