当我拖动一个DIV时,如何传递一个id的值给一个函数

How to pass value of a id to a function when I drag a DIV?

本文关键字:一个 id 函数 何传递 DIV 拖动      更新时间:2023-09-26

这是我的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做同样的事情

使用给定的标记,您可以

$(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");
                }
            })
        }
    });
});

你的意思是:
Javascript添加:

var id = $(this).attr("id");
//...
data:{
    x: x,
    y: y,
    id: id
} 
php:

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