2014-02-27 19 views
1

MY jQuery代碼檢索 「id」 和 「數據」 值如何使用jQuery可拖動的事件

$(document).ready(function() { 
    $(".thumblink").draggable({ 
     stop: function(event, ui) { 
      var pos_x = ui.offset.left; 
      var pos_y = ui.offset.top; 
      console.log($(this).attr('id')); 
      console.log($(this).attr('data-need')); 
     $.ajax({ 
      type:"POST", 
      url:"save.php", 
      data:{x:pos_x,y:pos_y} 
      }); 

      }, 
     containment: "parent", 
     cursor: 'move' 
    }); 

我的HTML代碼;

<a href="" class="thumblink" data-need="12" "id="imglink1"><img src="rest/o.png width='50' height='50' border="0"/></a> 


console.log($(this).attr('id')); gives me null value 
console.log($(this).attr('data-need')); undefined value 
How to retrieve my anchor tag "id" and "data need" value please help. 

回答

2

使用this對象訪問ID

var ancId=$(this).attr('id'); var dataNeed=$(this).attr('data-need');

調用此您的draggable回調函數

快樂編碼:)的停止功能裏面

+0

對不起它沒有工作,當我console.log它給了我未定義的值 –

+0

你究竟在哪裏試圖訪問你的代碼中的ID?不要在ajax模塊中添加這些代碼行,將它放在'$(「.thumblink」).draggable({停止:函數(event,ui)} var ancId = $(this).attr('id' ); var dataNeed = $(this).attr('data-need');}' – dreamweiver

+0

是的,我確實做了同樣的事情,它給了我id空和dataneed undefined –

相關問題