Javascript 当我拖动DIV时,如何将id的值传递给函数?

Javascript 当我拖动DIV时,如何将id的值传递给函数?,javascript,html,drag,Javascript,Html,Drag,这是我的Javascript $(document).ready(function(){ $( ".ex" ).draggable({containment:'parent',cursor:'pointer',opacity:0.6, stop : function () { var x = $(this).position().left; var y = $(this).position().top; $.

这是我的Javascript

$(document).ready(function(){
   $( ".ex" ).draggable({containment:'parent',cursor:'pointer',opacity:0.6,




 stop : function () {
           var x = $(this).position().left;
           var y = $(this).position().top;

             $.ajax({
    url: "savedrag.php", /* You need to enter the URL of your server side script*/
    type: "POST",
      /* add the other variables here or serialize the entire form. 
      Image data must be URI encoded */
    data:{
            x: x,
            y: y
            },  
    success: function(msg)
    {
       alert("position save");
    }

        })
       } 
});

});
这是我的PHP

   <?php

$qry = "SELECT * from contact Where CustomerID='$pid'";
$result = mysql_query($qry);

while ($row = mysql_fetch_array($result)) 
{

 echo '<div class="ex">';
 $row["ContactID"];
 echo '</div>';

}


?>

如何将变量$row[“ContactID”]传递到每个函数中,以便每个可拖动的div在数据库中都有自己的独立位置


之前,我使用onclick通过按钮传入值(this);但是我不能对draggable做同样的事情。

你的意思是这样的:
Javascript添加:

var id = $(this).attr("id");

//...

data:{
    x: x,
    y: y,
    id: id
} 
php:

echo';
回声';

使用给定的标记,您可以

$(document).ready(function() {
    $(".ex").draggable({
        containment : 'parent',
        cursor : 'pointer',
        opacity : 0.6,

        stop : function() {
            var x = $(this).position().left;
            var y = $(this).position().top;

            $.ajax({
                url : "savedrag.php", 
                type : "POST",
                data : {
                    x : x,
                    y : y,
                    ContactID: $.trim($(this).html())
                },
                success : function(msg) {
                    alert("position save");
                }

            })
        }
    });

});

@您可以将其添加为
div
的属性。ex
,然后在js
$(this).attr('ContactID')
中会给出值。Discalimer:不确定PHP syntaxI是否无法获取带有“var id=$(this).attr(“id”);”的id,是否必须将其放在可拖动函数中?
$(document).ready(function() {
    $(".ex").draggable({
        containment : 'parent',
        cursor : 'pointer',
        opacity : 0.6,

        stop : function() {
            var x = $(this).position().left;
            var y = $(this).position().top;

            $.ajax({
                url : "savedrag.php", 
                type : "POST",
                data : {
                    x : x,
                    y : y,
                    ContactID: $.trim($(this).html())
                },
                success : function(msg) {
                    alert("position save");
                }

            })
        }
    });

});