$.post().success();的success自定义函数怎么传参数?

原来功能是,圈出来的是参数,
clipboard.png
想改成下面的这样

clipboard.png
这个参数不知道怎么传进去了...
应该怎么写呀

dellaa=function(thisItem){

alert(data.check);
if(data.check==1){
    //alert("删除成功");
    $(thisItem).parent().parent().parent().remove();       //删除item
    showmsg(id+"删除成功");
}else{
    //alert("删除失败");
}
                        

}

Items.prototype.ajaxComm=function (obj,traget,query) {

   $(obj).each(function(){
                $(this).click(function(){
                    var id=$(this).parent().parent().parent().attr("id");       //获取ID
                    //console.log(id);
                    target=window.HOST_URL+traget;//"/Home/items/"+actionname
                    console.log(obj,target);
                    //alert(window.HOST_URL);
                    //var query={};                      //定义键值对(数组)
                    query["id"]=id;                      //定义键值对
                    var thisItem=this;
                    $.post(target,query).success(dellaa);

                });
    });

}

阅读 7.9k
3 个回答

要实现传参数的话,
你可以利用闭包

dellaa=function(data,thisItem){
    alert(data.check);
    if(data.check==1){
        //alert("删除成功");
        $(thisItem).parent().parent().parent().remove();       //删除item
        showmsg(id+"删除成功");
    }else{
        //alert("删除失败");
    }
                            
}
...
var thisItem=this;
$.post(target,query).success(function(data){
    dellaa(data,thisItem);
});

或者利用函数的bind方法

dellaa=function(data){
    var thisItem=this;
    alert(data.check);
    if(data.check==1){
        //alert("删除成功");
        $(thisItem).parent().parent().parent().remove();       //删除item
        showmsg(id+"删除成功");
    }else{
        //alert("删除失败");
    }
                            
}
...
$.post(target,query).success(dellaa.bind(thisItem));

你可以参考下这个API文档:http://api.jquery.com/jquery.post/
success接收的是回调函数,
看函数定义
Type: Function( Object data, String textStatus, jqXHR jqXHR )
一般传入data参数就够了

变量作用域的问题,第一个之所以可以,是因为success里的匿名函数写在click里面,所以这个匿名函数的父作用域中有thisItem这个局部变量,而后面那个之所以不行,是因为success的回调函数定义在了click外,自然不能访问到click里的变量了。
另外,不对齐的代码不能看...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题