[英]How to pass id, name & value of a div tag to a javascript function
[英]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.