如何在$.ajax()方法的success回调方法中获取当前对象?

guan_tu 2014-08-03 11:47:15

$("h4").toggle(function() {
var obj = $(this).siblings("div:gt(0)");//此处$(this)代表的是当前的$("h4")对象
$.ajax({
url : "showAjax.action",
type : "get",
data : "id=" + $(this).prev().val(),
success : function(result) {
// 如果在此处使用$(this)和在外面使用$(this)代表的不是同一个对象
obj.html(result).show();
},
error : function() {
alert("发生错误!");
}
});
}, function() {
var obj = $(this).siblings("div:gt(0)");
obj.hide();
});

请问 1.在success的function中如何直接获取$("h4")这个对象?
2.在success的function()中使用$(this)是指的哪个对象?
在此先谢谢各位了 !
...全文
9914 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn_ysza 2018-07-10
  • 打赏
  • 举报
回复
superwait_at18 2016-05-14
  • 打赏
  • 举报
回复
$("h4").toggle(function() { var _this=this; var obj = $(this).siblings("div:gt(0)");//此处$(this)代表的是当前的$("h4")对象 $.ajax({ url : "showAjax.action", type : "get", data : "id=" + $(this).prev().val(), success : function(result) { $(_this); }, error : function() { alert("发生错误!"); } }); }, function() { $(_this); }); 触发事件后先把指向dom用个变量记下来,后面的回调函数就可以使用了
Go 旅城通票 2014-08-05
  • 打赏
  • 举报
回复
$.ajax就是一个方法而且,然后将alert的返回值作为$.ajax的参数。。和jquery没关系。。
    function abc(p) {
        alert('调用abc的方法传递的参数P:'+p);
    }


    abc(alert('执行alert后返回值作为abc的参数'))
guan_tu 2014-08-04
  • 打赏
  • 举报
回复
引用 5 楼 xuzuning 的回复:
既然知道,怎么就绕不过弯子来呢? 你传递给 $.ajax 的不是一个对象吗?在这个对象里 this 不就是对象本身吗? 你 alert(JSON.stringify(this)) 和 alert(JSON.stringify($(this))) 不就观察到了吗
额,突然间想通了,{}中的this就是指{}这个对象本身,不过突然间发现$.ajax()方法中可以写任意的代码例如:

$(function() {
		$("p").click(function() {
			$.ajax(
				alert(1),alert(2)
			);
		});
	});
这样写与我直接在function(){alert(1);alert(2)}似乎没有什么区别,而且类似于$.get,$.post这种jQuery的全局函数好像里面都可以这样写啊 ,之前一直以为$.ajax()就是单纯的发送异步请求的一个函数,没想到还可以这样使用哪 !
豪情 2014-08-04
  • 打赏
  • 举报
回复
引用 2 楼 nd707355117 的回复:
嗯,谢谢,不过第二个问题还有疑惑! 我的意思是指,如果我在success的回调函数里 使用了 $(this),那么这个$(this)到底是 哪个对象呢?
版主息怒,哈哈。 我在1楼的时候就说了指向的传参的这个对象。只是当时回答问题比较着急,没有完全的重现这句话的意思, 看下面的例子:

var obj = {
    url : "showAjax.action",
    type : "get",
    data : "id=" + $(this).prev().val(),
    success : function(result) {
        alert(this.type);
        // 如果在此处使用$(this)和在外面使用$(this)代表的不是同一个对象
    },
    error : function() {
        alert("发生错误!");
    }
}

obj.success();
xuzuning 2014-08-04
  • 打赏
  • 举报
回复
既然知道,怎么就绕不过弯子来呢? 你传递给 $.ajax 的不是一个对象吗?在这个对象里 this 不就是对象本身吗? 你 alert(JSON.stringify(this)) 和 alert(JSON.stringify($(this))) 不就观察到了吗
guan_tu 2014-08-03
  • 打赏
  • 举报
回复
引用 3 楼 xuzuning 的回复:
$(this) 是 this 的 JQuery 包装 而 this 是 ajax 对象本身
额,你说的我知道,我的意思是

$(function() {
$("h3").click(function(){
			alert($(this).html());// 此处为第一句(能够正常获取到h3内的文本)
			$.ajax({
				url : "/json1/showAllEmployees1.action",
				type : "get",
				data : null,
				dataType : "json",
				success : function(result) {
					alert($(this).html()); //此处为第二句 (总是弹出undefined)
					$.each(result,function(i,obj){
						alert(obj.dept.dname);
					});
				},
				error : function() {
					alert("发生错误!");
				}
			});
		});
	});
页面 只有一个h3标签如下: <h3>测试</h3> 经过测试 第一句能够正常获取到 “测试”,而第二局不能获取到 ,所以我想问的是 第二句中的$(this)已经不是原来的$("h3")那个对象了 ,现在变成了哪个对象呢?
xuzuning 2014-08-03
  • 打赏
  • 举报
回复
$(this) 是 this 的 JQuery 包装 而 this 是 ajax 对象本身
guan_tu 2014-08-03
  • 打赏
  • 举报
回复
嗯,谢谢,不过第二个问题还有疑惑! 我的意思是指,如果我在success的回调函数里 使用了 $(this),那么这个$(this)到底是 哪个对象呢?
豪情 2014-08-03
  • 打赏
  • 举报
回复
直接取不到,非要在外边缓存一下才能取到。self。 里边的this指向的是那个对象:

{
        url : "showAjax.action",
        type : "get",
        data : "id=" + $(this).prev().val(),
        success : function(result) {
            // 如果在此处使用$(this)和在外面使用$(this)代表的不是同一个对象
            obj.html(result).show();
        },
        error : function() {
            alert("发生错误!");
        }
    }

$("h4").toggle(function() {
    var self = $(this);
    var obj = $(this).siblings("div:gt(0)");//此处$(this)代表的是当前的$("h4")对象
    $.ajax({
        url : "showAjax.action",
        type : "get",
        data : "id=" + $(this).prev().val(),
        success : function(result) {
            // 如果在此处使用$(this)和在外面使用$(this)代表的不是同一个对象
            obj.html(result).show();
        },
        error : function() {
            alert("发生错误!");
        }
    });
}, function() {
    var obj = $(this).siblings("div:gt(0)");
    obj.hide();
});

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧