ajax回调后无法更改内部文本?

sal*_*lar 5 ajax asp.net-mvc jquery

我在页面上有十个像按钮,我是通过页面上的foreach循环创建的.

我在使用jQuery更改内部文本时遇到问题.

让我用我的元素来解释

     <a class="pr-endorse" id="<%=product.ConfectionaryProductId %>">
        <i class="pr-add"></i>
        <span>+</span>
        <span class="pr-likes"><%=product.EndorsementCount %></span>
    </a>
Run Code Online (Sandbox Code Playgroud)

这是我喜欢的按钮元素之一.

这是提交用户的Ajax函数

      $(document).ready(function () {
        $(".pr-endorse").click(function () {
            debugger;
            var productId = $(this).attr("id");
            var confId = $("#ConfectioneryId").val();
            var countNumber = $(this).find(".pr-likes").html();
            $.ajax({
                url: '<%: Url.Action("Endorsement","Confectionery")%>',
                data: { productId: productId, itemId: confId },
                type: "POST",
                async: true,
                cache: false,
                success: function (result) {

                    debugger;
                    $(this).find(".pr-likes").text(result.endoresCount);
                    alert(result.endoresCount);

                },
                error: function (xhr) {
                    alert(xhr);
                }
Run Code Online (Sandbox Code Playgroud)

我认为这部分代码应该解决

  $(this).find(".pr-likes").text(result.endoresCount);
Run Code Online (Sandbox Code Playgroud)

但它不起作用?

ade*_*neo 1

我会尝试一下,它可能看起来像这样

$(document).ready(function () {
    $(".pr-endorse").click(function () {

        var self        = this
        var productId   = self.id;
        var confId      = $("#ConfectioneryId").val();
        var countNumber = $(this).find(".pr-likes").html();

        $.ajax({
            type : "POST",
            url  : '<%: Url.Action("Endorsement","Confectionery")%>',
            data : {
                productId : productId,
                itemId    : confId
            },
            dataType : 'json',
            success : function (result) {

                $(self).find(".pr-likes").text(result.endoresCount);
                alert(result.endoresCount);

            },
            error : function (xhr) {
                alert(xhr);
            }
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

请注意,this成功函数内部不是被单击的元素,并且要返回一个对象,您通常需要设置 dataType,即使 jQuery 在标题正确的情况下会解析它。