如何使用Jquery Draggable事件检索“id”和“data”值

时间:2014-02-27 11:05:28

标签: jquery jquery-ui javascript-events jquery-ui-draggable

MY 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.

1 个答案:

答案 0 :(得分:2)

使用this对象访问ID

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

draggable回拨功能

的停止功能中调用此功能

快乐编码:)