jQuery的$。阿贾克斯:通过额外的参数'成功'回调回调、参数、jQuery、阿贾克斯

由网友(青橘栀耳)分享简介:我使用$阿贾克斯将数据发布到服务器。不过,我想通过一个额外的参数的'成功'回调告诉回调函数的HTML元素的反应是对的ID。这是可能的吗?像:success_cb(数据,elementid){(更新与返回服务器数据的elementid)}$阿贾克斯({...成功:success_cb(elementid)});解决...

我使用$阿贾克斯将数据发布到服务器。不过,我想通过一个额外的参数的'成功'回调告诉回调函数的HTML元素的反应是对的ID。

这是可能的吗?像:

  success_cb(数据,elementid)
{
    (更新与返回服务器数据的elementid)
}

$阿贾克斯({
    ...
    成功:success_cb(elementid)
});
 

解决方案

 函数postForElement(elementId){
  .post的$('/富',someValues​​,功能(数据){
    $(elementId)。html的(返回的服务器:+数据);
  },'JSON');
}
 

通过声明函数文本在同一范围内的 elementId 局部变量,函数变成的关闭的有权访问该局部变量。 (或者有些人可能会说,这只是成为一个的关闭的时候,函数文本还引用了一个未在其范围定义的非全局变量。这只是bandying文字。)

8月,来富力足校,体验富力 阿贾克斯的魅力吧

I am using $.ajax to post data to the server. However I want to pass an additional parameter to the 'success' callback to tell the callback function the id of the HTML element the response is for.

It is possible? Like:

success_cb(data, elementid)
{
    (update the elementid with the server returned data)
}

$.ajax({
    ...
    success:success_cb(elementid)
});

解决方案

function postForElement(elementId){
  $.post('/foo',someValues,function(data){
    $(elementId).html("The server returned: "+data);
  },'json');
}

By declaring the function literal in the same scope as the elementId local variable, the function becomes a closure that has access to that local variable. (Or some might say it only becomes a closure when the function literal also references the non-global variable that is not defined in its scope. That's just bandying with words.)

阅读全文

相关推荐

最新文章