jQuery的ajax的成功不以$(这)工作? [英] jQuery ajax success doesn't work with $(this)?

查看:76
本文介绍了jQuery的ajax的成功不以$(这)工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直都玩jQuery中的Ajax工具时遇到使用$(这)我的AJAX的执行成功的内部问题。我在想,如果可以使用$(本),您的成功里面为我见过的教程使用它,但是当我尝试使用它它不工作...但是如果我使用$(文件)或其他方法去我想它正常工作的对象...任何帮助将不胜pciated,因为我很新的jQuery的AP $ P $!提前致谢!在code IM摆弄如下:

  $(markRead)点击(函数(){
    。VAR CID = $(本)。家长(分区)父母(分区)发现(#CID)VAL()。
    VAR字段=IsRead;

    $阿贾克斯({
        键入:POST,
        网址:AJAX / contract_buttons.php
        数据类型:文本,
        数据:contractId =+ CID +与& UPDATEFIELD =+字段,
        异步:假的,
        成功:函数(响应){
            // $(本)犯规,在成功的功能时,可以识别调用对象...
            $(本).find(IMG)ATTR(SRC,图像/ read.png)。
        },
        错误:函数(XHR,ajaxOptions,thrownError){
            警报(xhr.statusText);
            警报(thrownError);
        }
    });
});
 

解决方案

总是指向当前执行上下文,因此并不一定停留在一个回调函数相同像阿贾克斯成功处理程序。如果您想引用它,你必须只是做丹尼斯指出,它的值保存到自己的局部变量,因此您可以在以后引用它,即使在实际值可能已被设置为别的东西。这绝对是JavaScript的细微差别之一。改变你的code到这一点:

  $(markRead)点击(函数(){
    。VAR CID = $(本)。家长(分区)父母(分区)发现(#CID)VAL()。
    VAR字段=IsRead;
    VAR元=这一点; //保存在回调以后使用

    $阿贾克斯({
        键入:POST,
        网址:AJAX / contract_buttons.php
        数据类型:文本,
        数据:contractId =+ CID +与& UPDATEFIELD =+字段,
        异步:假的,
        成功:函数(响应){
            // $(本)犯规,在成功的功能时,可以识别调用对象...
            $(元素).find(IMG)ATTR(SRC,图像/ read.png)。
        },
        错误:函数(XHR,ajaxOptions,thrownError){
            警报(xhr.statusText);
            警报(thrownError);
        }
    });
});
 

Ive been playing with the ajax tools in jQuery and am having an issue with using $(this) inside the success of the execution of my ajax. I was wondering if its possible to use $(this) inside of your success as ive seen tutorials use it but when i attempt to use it it doesnt work... However if i use $(document) or some other method to get to the object i want it works fine... Any help would be greatly appreciated as i am quite new to jQuery! Thanks in advance! The code im playing with is as follows:

$(".markRead").click(function() {
    var cId = $(this).parents("div").parents("div").find("#cId").val();
    var field = "IsRead";

    $.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...
            $(this).find("img").attr("src", "images/read.png");
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(xhr.statusText);
            alert(thrownError);
        }
    });
});

解决方案

this always refers to the current execution context so it does not necessarily stay the same in a callback function like an ajax success handler. If you want to reference it, you must just do as Dennis has pointed out and save its value into your own local variable so you can reference it later, even when the actual this value may have been set to something else. This is definitely one of javascript's nuances. Change your code to this:

$(".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);
        }
    });
});

这篇关于jQuery的ajax的成功不以$(这)工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆