如何访问放入可排序的可拖动元素的ID
我正在使用 JQuery 库来实现拖放。
当将元素放入可排序列表时,如何获取正在拖动的元素?
我想获取被拖动的div的id。 拖动以下元素:
<div class="control" id="control[1]" >
<img src="img/controls/textfield.png" />
</div>
我从他们的示例函数中获得了标准拖动功能,
$(".control").draggable({
connectToSortable: '#sortable',
helper: 'clone'
});
在拖动部分停止,下一个代码返回正确的值
stop: function(event, ui) {
alert(ui.helper.attr('id'));
}
这是可排序的元素:
<ul id="sortable"><li>test</li></ul>
和他的功能:
$("#sortable").sortable({
revert: true,
accept: '.control',
receive: function(event, ui) {
// here i need to get draggable element id
}
});
我尝试了各种 ui.id 等,但似乎没有工作。
receive: function(event, ui) {
$(ui.draggable).attr("id")
}
抛出未定义
。
更新:
抱歉,是我的错:) 正如我母亲常说的那样 - “在编码之前阅读 API”。 ui.item.attr('id')
工作正常。
I am using the JQuery libs to implement drag and drop.
How do I get at the element that is being dragged when it is dropped into sortable list?
I want to get the id of the div being dragged. The following element is dragged:
<div class="control" id="control[1]" >
<img src="img/controls/textfield.png" />
</div>
I have the standard dragging function from their example
$(".control").draggable({
connectToSortable: '#sortable',
helper: 'clone'
});
function stop in dragging section with next code return proper value
stop: function(event, ui) {
alert(ui.helper.attr('id'));
}
And this is sortable element:
<ul id="sortable"><li>test</li></ul>
and his function:
$("#sortable").sortable({
revert: true,
accept: '.control',
receive: function(event, ui) {
// here i need to get draggable element id
}
});
I have tried various ui.id etc which doesn't seem to work.
receive: function(event, ui) {
$(ui.draggable).attr("id")
}
throws undefined
.
Update:
Sorry, my fault :) As my mother used to tell - "Read API before coding". ui.item.attr('id')
works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
尝试
根据文档,接收(实际上是可排序的所有回调)获取两个参数。 第二个参数应包含:
元素(通常是
item)
helper
助手的
您定义了一个)
项目来自(仅当您存在时才存在)
从一个连接列表移动到
其他)
Try
According to the documentation the receive (indeed all callbacks for sortable) get two arguments. The second argument should contain:
element (most often a clone of the
item)
helper
of the helper
you defined one)
item comes from (only exists if you
move from one connected list to
another)
看起来 ui.item 的
context
在beforeStop
事件和receive
事件之间发生了变化。就我而言,我尝试将项目的 id 设置为生成的值,但
对我不起作用,
因此您可以解决此问题:
It seems like the
context
of ui.item changes between thebeforeStop
event andreceive
event.In my case I'm trying to set the id of the item to a generated value and
doesn't work for me
So you can work around this :
我遇到了类似的问题,并且在访问开始事件中的帮助器元素时遇到了问题。 我最终所做的是将 helper 属性设置为返回一些自定义 HTML 的函数。 我能够在开始事件中访问该 HTML,没有任何问题。
I had a similar problem, and had troubles accessing the helper element in the start event. What I ended up doing was setting the helper attribute to a function which returned some custom HTML. I was able to access that HTML in the start event without any problems.
是的,当您混合可排序、可拖动、可放置等时,这是一个问题。
使可排序容器也可放置:
这应该可行
Yes, that's a problem when you mixing sortables draggables droppables etc.
Make sortable container as droppable as well:
that should work
来自 jQuery UI 可拖动文档:
请参阅http://docs.jquery.com/UI/API/1.7/Droppable
From the jQuery UI draggable docs:
See http://docs.jquery.com/UI/API/1.7/Droppable
我成功地使用了 beforeStop 事件进行排序。
来自此处
I had success using the beforeStop event for the sortable.
From here
看起来有点老套 - 但成功了! 我必须使用 $('.dragrow1 li:last')
Seems a bit hacky - but worked it out! I have to use $('.dragrow1 li:last')
您可以使用自定义数据属性来存储拖动项目的 ID。 该属性仍然存在于掉落的物品上。 源代码如下:
在可排序中使用
$('dropped item").data('id')
来获取 id,如下所示:You can use a custom data attribute to store the id of the dragged items. This attribute is still there on the dropped items. The source code as following:
Using
$('dropped item").data('id')
in the sortable to get the id like: