栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

jQuery ajax成功不适用于$(this)吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jQuery ajax成功不适用于$(this)吗?

this
总是引用当前的执行上下文,因此它不必像ajax成功处理程序那样在回调函数中保持不变。如果要引用它,则必须按照Dennis所指出的那样进行操作,并将其值保存到自己的局部变量中,以便以后可以引用它,即使实际
this
值可能已设置为其他值。这绝对是javascript的细微差别之一。将代码更改为此:

$(".markRead").click(function() {    var cId = $(this).parents("div").parents("div").find("#cId").val();    var field = "IsRead";    var element = this;   // save for later use in callback    $.ajax({        type: "POST",        url: "ajax/contract_buttons.php",        dataType: "text",        data: "contractId=" + cId + "&updateField=" + field,        async: false,        success: function(response) { //$(this) doesnt recognize the calling object when in the success function... $(element).find("img").attr("src", "images/read.png");        },        error: function(xhr, ajaxOptions, thrownError) { alert(xhr.statusText); alert(thrownError);        }    });});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/636658.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号