draggable
交换两个元素时,我无法从 mysql 获取数据。因为交换动作是在$.post
函数内部执行的。
$(function () {
var parent_div_data;
var field_array = ["tire_number", "bar", "mm_1", "mm_2", "mm_3"];
$(".tire").draggable({
containment: ".table_edit_data",
scroll: false,
appendTo: "body",
cursor: "move",
helper: 'clone',
revert: "invalid",
start: function (event, ui) {
parent_div_data = $(this).parent().attr("id");
},
stop: function (event, ui) {
setTimeout(function () {
if ($('#' + parent_div_data).children().length == 0) {
$.each(field_array, function (index, value) {
$('#' + parent_div_data).parent().find('.' + value).val('');
});
}
}, 50);
}
});
$("#tire_div").droppable({
tolerance: "intersect",
accept: ".tire",
drop: function (event, ui) {
$("#tire_div").append($(ui.draggable));
}
});
$(".droppable_1, .droppable_2, .droppable_3, .droppable_4, .droppable_5, .droppable_6").droppable({
tolerance: "intersect",
accept: ".tire",
activeClass: "div-droppable-default",
hoverClass: "div-droppable-hover",
drop: function (event, ui) {
var url = 'class=<?= $this->reg['
class '] ?>&"&action=get_tire_data';
var this_data = $(this);
$.post('index.php?' + url, {
tire_id: $(ui.draggable).attr('data')
}, function (data) { //data attribute contains `id`. And data is returned.
if (this_data.find(".tire").length == 1) {
var first = this_data.find(':first-child'); //where elements swap is performed
$('#' + parent_div_data).append(first);
// $.each(field_array, function(index, value) {
// $('#' + parent_div_data).parent().find('.' + value).val(first.attr(value));
// });
}
this_data.append($(ui.draggable));
this_data.parent().find('.tire_number').val(data.tire_number);
this_data.parent().find('.bar').val(data.bar);
this_data.parent().find('.mm_1').val(data.mm_1);
this_data.parent().find('.mm_2').val(data.mm_2);
this_data.parent().find('.mm_3').val(data.mm_3);
}, "json");
}
});
});
在执行交换操作的位置(请参阅代码中的注释),我是否需要添加另一篇文章以再次获取值,或者您能否建议另一种方法。谢谢。