2012-01-03 66 views
1

我有這樣的代碼:嘗試使用數據()

$('.solicitud_amistad').click(function(){ 
    var aux = $(this); 
    console.log($(this)); 
    console.log($(this).data('receptor')); 
    $.post('amistad/nueva' , {id_receptor:$(this).data('receptor')}, 
     function(response){ 
      aux.replaceWith(response) 
     } 
    ); 
}); 

和我得到這樣的輸出:

<a class="solicitud_amistad" data-receptor="4">Enviar solicitud de amistad</a> 
undefined 

我期望的4而不是undefined

+0

你的例子適合我。 http://jsfiddle.net/M7JjF/ – 2012-01-03 20:38:10

+0

您正在使用.data('受體'),但元素上的關鍵是數據受體? – osoner 2012-01-03 20:39:13

+0

也適用於我http://jsfiddle.net/qKuZq/ - 您使用的是哪個版本的jquery? – swatkins 2012-01-03 20:40:26

回答

0

嘗試:

jQuery.data(this, 'receptor'); 

如果不工作,那麼你是無法正確設置數據,需要你發佈了存儲數據對稱爲受體的關鍵。