首页 > 技术文章 > $(this)在ajax中无效的解决方案

ch-zaizai 2016-12-12 11:32 原文

在ajax方法里写$(this)指向的是最近调用它的jquery对象,所以这里的$(this)指的是ajax对象,而不是$(".enter_caozuo").find(".gd"),要想解决这个问题,就需考虑增加一个中间变量传递这个对象,示例:
$(".enter_caozuo").find(".gd").click(function(){
var mythis = $(this);

$.ajax({
type:"GET",
url:"lanEdit.php?lanname="+lan_name,
success:function(msg){
var info = eval('('+msg+')');
var state = info.state;
mythis.parents(".yuan").siblings(".more").find(".lanname").val(info.name);
//....你的代码

}

推荐阅读