jQuery Draggables - 获取可拖动的 id
找到解决方案! 我发现了它,我忽略了我只能获取变量项的 id。所以我输入了 var item_id = item.attr('id');
这就是我的解决方案。比我想象的简单得多。感谢大家的帮助!
好的,我在 jQuery 中有多个可拖动元素,它们都是图像,但每个元素都有自己的 ID。不管怎样,我已经设置了一个函数,一旦图像被拖放到可放置的物体上,就会执行该函数。我希望能够提取图像的 ID,以便我可以处理数据库中已删除的图像。
function foundationsInSpot(drag_item,spot)
{
var oldSpotItem = $(spot).find('img');
if(oldSpotItem.length>0) {
oldSpotItem.appendTo('#foundationinv').draggable({ revert: 'invalid' });
}
var item = $('<img />');
var item_id = "";
var bid = "<?= $bid ?>";
item.attr('src',drag_item.attr('src')).attr('id',drag_item.attr('id')).attr('class',drag_item.attr('class')).appendTo(spot).draggable({ revert: 'invalid' });
drag_item.remove();
alert('BenID: ' + bid + ' Foundation: ' + item_id);
var dataString = 'item_id='+ item_id + '&bid=' + bid;
$.ajax({
type: "POST",
data: dataString,
url: "http://motb.isgreat.org/objects/pfoundations.php",
});
}
这就是我的代码。我需要找到 item_id
。这是我的猜测 var item_id = $('').attr('id');
但它返回了空白信息。我还尝试了 var item_id = ui.draggable.attr("id"); ,这只是停止了我的函数的其余部分。有什么想法吗?提前致谢。
顺便说一句,这是我的可拖动项目的样子。
更新 我将 var item_id = ui.draggable.attr("id");
移至 if 语句上方,现在我得到 undefined
。所以我不确定接下来该去哪里,但我知道我的 AJAX 调用运行正常,因为它正在用 undefined
更新我的数据库。
更新 - 再次
所以这就是我如何调用我的函数,如果这样更容易的话。
$(".foundations").draggable({ revert: 'invalid'}).addTouch;
$('#foundationinv').droppable({accept: '.foundations'});
$("#foundations").droppable({ accept: '.foundations'})
$('#foundations,#foundationinv').bind('drop', function(ev,ui) { foundationsInSpot(ui.draggable,this); });
我以前从未做过这样的事情,所以这真的超出了我的能力范围。如果您想亲自查看该页面,请访问 http://motb.isgreat.org/ 并单击登录按钮,然后使用 testbot 作为用户名和密码。要查看可拖动项的位置,左侧有一个大框,下面有 4 个较小的框,单击小框上方的“基础”一词。我试图将出现的那些对象拖到小框中。您可以看到一条警报,其中包含我试图传递的信息。请随意检查我的源代码是否有任何问题,这些项目是从我的数据库中调用的,因此您的源代码中可能缺少我的 PHP。
我发现了它,我忽略了我只能获取变量项的 id。所以我把 var item_id = item.attr('id');
这就是我的解决方案。比我想象的简单得多。感谢大家的帮助!
Solution Found!
I found it, I was overlooking that I could just get the id of the variable item. So I put var item_id = item.attr('id');
and that was my solution. So much simpler than I thought. Thanks for your help everyone!
Okay, so I have multiple draggable elements in jQuery, they are all images, but each one has their own ID. Anyway, I have set up a function that is preformed once the image is dropped onto the droppable. I want to be able to pull the id of the image so I may process what image has been dropped in my database.
function foundationsInSpot(drag_item,spot)
{
var oldSpotItem = $(spot).find('img');
if(oldSpotItem.length>0) {
oldSpotItem.appendTo('#foundationinv').draggable({ revert: 'invalid' });
}
var item = $('<img />');
var item_id = "";
var bid = "<?= $bid ?>";
item.attr('src',drag_item.attr('src')).attr('id',drag_item.attr('id')).attr('class',drag_item.attr('class')).appendTo(spot).draggable({ revert: 'invalid' });
drag_item.remove();
alert('BenID: ' + bid + ' Foundation: ' + item_id);
var dataString = 'item_id='+ item_id + '&bid=' + bid;
$.ajax({
type: "POST",
data: dataString,
url: "http://motb.isgreat.org/objects/pfoundations.php",
});
}
So that's my code. I need to find item_id
. Here was my guess var item_id = $('<img />').attr('id');
but it returned blank information. I also tried var item_id = ui.draggable.attr("id");
and that just stopped the rest of my function. Any ideas? Thanks in advance.
By the way here is how my draggable items look like.<img src='http://motb.isgreat.org/objects/khhhqw4s.png' class='object foundation' id='khhhqw4s'/>
UPDATE
I moved the var item_id = ui.draggable.attr("id");
above the if statement, and now I get undefined
. So I am unsure where to go from here, but I am aware my AJAX call is operating correctly as it is updating my database with undefined
.
UPDATE - Again
So here is how I am calling my function if this makes it easier.
$(".foundations").draggable({ revert: 'invalid'}).addTouch;
$('#foundationinv').droppable({accept: '.foundations'});
$("#foundations").droppable({ accept: '.foundations'})
$('#foundations,#foundationinv').bind('drop', function(ev,ui) { foundationsInSpot(ui.draggable,this); });
I have never done anything like this before, so It's really beyond me. If you would like to see the page in person, go to http://motb.isgreat.org/ and click the login button, and use testbot for both the username and the password. To see where the draggables are, on the left side there is this big box with 4 smaller ones below it, click above the small box where the word is foundations. I am trying to drag those objects that appear into the small box. You can see an alert with the information I am trying to pass. Feel free to check my source for any issues, the items are called up from my database, so my PHP is probably missing from your source.
I found it, I was overlooking that I could just get the id of the variable item. So I put var item_id = item.attr('id');
and that was my solution. So much simpler than I thought. Thanks for your help everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
droppable
中,您有一个名为drop
的属性,使用该方法来调用调用
$(ui.draggable).attr("id")< 删除的对象/code>
如果你看到该对象,
ui.draggable
只是 HTML,很快你用$()
包装它,你将能够用它执行任何 jQuery 操作。在 Inspector 中打开控制台以查看 id
inside the
droppable
you have a property calleddrop
use that method to call what object was dropped calling
$(ui.draggable).attr("id")
if you see the object,
ui.draggable
is only the HTML, soon you wrapp it with$()
you will be able to do any jQuery operation with it.open the Console in Inspector to see the id's