SORU
18 HAZİRAN 2011, CUMARTESİ


(bu) $başarı çalışmıyor AJAX içinde

Ben(bu) $kullanmak bir çok bu özelliği kullanan bazı eski kodunu değiştirmek için çalışıyorum. Sorun(bu) $zaman başarı içinde çalışmıyor. Zaten yapacak bir var olarak ayarlamadan bu var.

$('.addToCart').click(function() {

    $.ajax({
        url: 'cart/update',
        type: 'post',
        data: 'product_id='   $(this).attr("data-id"),
        dataType: 'json',
        success: function(json) {

            if (json['success']) {

            $(this).addClass("test");

            }   
        }
    });

});

CEVAP
18 HAZİRAN 2011, CUMARTESİ


Sorun

Geri içinde, this Ajax arama jqXHR nesne anlamına gelir, değil öğesi olay işleyicisi belliydi. Learn more about how this works in JavaScript.


Çözümler

context option ayarlayabilirsiniz:

Bu nesne Ajax ile ilgili tüm kapsamında geri verilecek. Varsayılan olarak, içerik ajax ayarları çağrısında kullanılan ($.ajaxSettings ayarlar $.ajax geçmiş ile birleşti) temsil eden bir nesnedir.(...)

$.ajax({
    //...
    context: this,
    success: function(json) {
         // `this` refers to the value of `context`
    }
});

veya $.proxy kullanın:

$.ajax({
    //...
    success: $.proxy(function(json) {
         // `this` refers to the second argument of `$.proxy`
    }, this)
});

ya geri dışında this değeri için bir başvuru tutun:

var element = this;

$.ajax({
    //...
    success: function(json) {
         // `this` refers to the jQXHR object
         // use `element` to refer to the DOM element
         // or `$(element)` to refer to the jQuery object
    }
});

İlgili

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • GoldgenieOfficial

    GoldgenieOff

    23 Temmuz 2009
  • metallmanutza13

    metallmanutz

    13 NİSAN 2007
  • The Fashion Sight

    The Fashion

    22 AĞUSTOS 2011