當我拖動多個項目並將其放到特定div時,它只取第一個元素。後來當我檢查console.log($(ui.draggable))長度爲1.在jquery中拖動元素
({selector:"", context:#1=({}), 0:#1#, length:1}) when i dragged 3 elements
請幫助我哪裏出錯了?
http://jsfiddle.net/akkiys/Hkfh2/4/
的HTML看起來像這樣:
<ul id="drag" >
<li id="1">one</li>
<li id="2">two</li>
<li id="3">three</li>
<li id="4">four</li>
<li id="5">five</li>
</ul>
和JavaScript:
var selectedClass = 'ui-state-highlight',
clickDelay = 600,
// click time (milliseconds)
lastClick, diffClick; // timestamps
$("#drag li")
// Script to deferentiate a click from a mousedown for drag event
.bind('mousedown mouseup', function(e) {
if (e.type == "mousedown") {
lastClick = e.timeStamp; // get mousedown time
} else {
diffClick = e.timeStamp - lastClick;
if (diffClick < clickDelay) {
// add selected class to group draggable objects
$(this).toggleClass(selectedClass);
}
}
}).draggable({
scroll: false,
revertDuration: 10,
// grouped items animate separately, so leave this number low
start: function(e, ui) {
ui.helper.addClass(selectedClass);
},
stop: function(e, ui) {
// reset group positions
$('.' + selectedClass).css({
top: 0,
left: 0
});
},
drag: function(e, ui) {
// set selected group position to main dragged object
// this works because the position is relative to the starting position
$('.' + selectedClass).css({
top: ui.position.top,
left: ui.position.left
});
}
});
$(".drop").droppable({
drop: function(event, ui) {
var droppableId = $(this).find('div.inner').attr("id");
//var id = ui.draggable.attr("id"); // i need to retrive multiple id
console.log($(ui.draggable))
var m_id = [];
$.each($(ui.draggable), function(i, e) {
m_id.push(e.id);
alert(m_id);
});
console.log(m_id);
}
});
使用您的jsfiddle,在Chrome中,我得到一個警告,指出有多少拖,和這說正確的數字。那不是你所看到的嗎? – Grezzo
@Grezzo請嘗試拖動多個項目並檢查。我需要拖動的所有多個值。我認爲你拖動單個項目。 – Akshay
你是什麼意思「拖動多個項目」?只要我放開,他們就會回到原來的位置。提供一些確切的步驟,以及您在每一步看到的和希望看到的內容。 – Grezzo