Javascript 如何获取jQuery可排序列表顺序?
我使用jquerysortable允许用户在页面上拖放元素。当用户拖动div时,我需要获取列表的更新顺序并将其传递到后端。到目前为止,我已经尝试:Javascript 如何获取jQuery可排序列表顺序?,javascript,jquery,jquery-ui,jquery-ui-sortable,Javascript,Jquery,Jquery Ui,Jquery Ui Sortable,我使用jquerysortable允许用户在页面上拖放元素。当用户拖动div时,我需要获取列表的更新顺序并将其传递到后端。到目前为止,我已经尝试: $( function() { $( "#sortable" ).sortable({ axis: 'y', stop: function (event, ui) { var data = $(this).sortable('serialize'); aler
$( function() {
$( "#sortable" ).sortable({
axis: 'y',
stop: function (event, ui) {
var data = $(this).sortable('serialize');
alert(data);
$.ajax({
data: oData,
type: 'POST',
url: '/url/here'
});
}
});
$( "#sortable" ).disableSelection();
} );
但这使得动画实际上并不平滑,并且没有数据发出警报。如何在用户每次拖放div时获得职位列表
相反,请使用
toArray()
方法,此处详述:相反,请使用toArray()
方法,此处详述:可以使用refreshPositions()
函数返回表示可排序项的对象。然后,您可以通过对该对象调用.children()
来获取更新的子对象列表
将位置保存到stop
事件中的变量,该事件在完成排序后触发
我已更新了您的函数以包含停止
事件:
$("#sortable").sortable({
stop: function(ev, ui) {
//Get the updated positions by calling refreshPositions and then .children on the resulting object.
var children = $('#sortable').sortable('refreshPositions').children();
console.log('Positions: ');
//Loopp through each item in the children array and print out the text.
$.each(children, function() {
console.log($(this).text().trim());
});
}
});
有一个
refreshPositions()
函数可用于返回表示可排序项的对象。然后,您可以通过对该对象调用.children()
来获取更新的子对象列表
将位置保存到stop
事件中的变量,该事件在完成排序后触发
我已更新了您的函数以包含停止
事件:
$("#sortable").sortable({
stop: function(ev, ui) {
//Get the updated positions by calling refreshPositions and then .children on the resulting object.
var children = $('#sortable').sortable('refreshPositions').children();
console.log('Positions: ');
//Loopp through each item in the children array and print out the text.
$.each(children, function() {
console.log($(this).text().trim());
});
}
});
感谢@Sterling为您提供的答案。尝试了同样的问题:在你的小提琴中,你需要调用排序表上的toArray方法,所以
$(“#sortable”).sortable(“toArray”)
另外,正在排序的项目,即li
没有ID。排序表的直接子项需要有ID才能使用toArray
谢谢@Sterling的回答。尝试了同样的问题:在你的小提琴中,你需要调用排序表上的toArray方法,所以$(“#sortable”).sortable(“toArray”)
另外,正在排序的项目,即li
没有ID。排序表的直接子级需要有ID才能使用toArray