繁体   English   中英

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

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

这是我的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)通过按钮传递值; 但是对于可拖动对象我不能做同样的事情。

使用给定的标记,您可以

$(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>';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM