目标是在回发时启动jQuery AJAX POST的相同控件

Target the same control that initiated a jQuery AJAX POST on postback

本文关键字:POST AJAX 控件 jQuery 启动 目标      更新时间:2023-09-26
function pageLoad() {

        $(".VoteUp").live("click", function () {
            var Id = $(this).attr("index");
            d = JSON.stringify({ "Id": Id })
            $.ajax({
                type: 'POST',
                url: '../API/Services.asmx/LikeId',
                data: d,
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: testFunc,
                error: errFunc
            });
            $(".VoteUp").each(function () {
                $(this).attr("disabled", "disabled");
            });
        });
        function testFunc(result, userContext, methodName) {
            if (result.d[0] == "true") {
                TheVoteUpControl.val("added");
            }
            else {
                TheVoteUpControl.val("deleted");
            }
            $(".VoteUp").each(function () {
                $(this).removeAttr("disabled");
            });
        }
        function errFunc() {
            alert('error');
        }

如何在成功时更改相同的.VoteUp控件的值?我似乎不知道该如何瞄准发起事件的控制组。$ (" .VoteUp ")

建议吗?

我能想到的唯一方法是在全局变量中缓存元素,并在成功回调中捕获它。我还分别在completebeforeSend回调中移动了$(".VoteUp")的启用和禁用

    $(".VoteUp").live("click", function (e) { 
        e.preventDefault();
        $this=$(this); <-- cache this in the global variable
        var Id = $(this).attr("index");
        d = JSON.stringify({ "Id": Id })
        $.ajax({
            beforeSend:function(){
              $(".VoteUp").attr("disabled", "disabled");
            },
            type: 'POST',
            url: '../API/Services.asmx/LikeId',
            data: d,
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: testFunc,
            error: errFunc
            complete:function(){
                  $(".VoteUp").removeAttr("disabled");
           }
        });
    });

    function testFunc(result, userContext, methodName) {
        if (result.d[0] == "true") {
            $this.val("added");
        }
        else {
            $this.val("deleted");
        }

    }