【问题标题】:jQuery Sortable List Not Reordering on createjQuery 可排序列表在创建时不重新排序
【发布时间】:2014-07-13 00:02:00
【问题描述】:

我正在尝试重新排序 jquery 中可排序的列表元素,当它根据它们在列表中的顺序创建时。

var CACHES = ["WayBack Machine", "Google Cache", "Coral CDN"];
var HTML = ["Google Cache", "WayBack Machine", "Coral CDN"];

$("#sortable").sortable({
    stop: function(event, ui) {
        console.log("New position: " + ui.item.index());
    },
    change: function(event, ui) {
        // When the item postion is changed while dragging a item, I want it's position
        console.log("New position: " + ui.item.index());
    },
    create: function(event, ui) {
        console.log("Sortable Created");

        var ul = $('#sortable');
        var li = ul.children('li').get();

        li.sort(function(a,b) {
            $(a).attr('id')
            var value = CACHES.indexOf($(a).attr('id')) < CACHES.indexOf($(b).attr('id')) ? 1 : -1
            console.log(value)
            return value
        });

        ul.append(li);
    }
});

var HTML 是它们在页面 HTML 中的顺序。 var CACHES 是我要放入它们的顺序。它似乎确实对元素重新排序,但不正确。

【问题讨论】:

    标签: javascript jquery jquery-ui


    【解决方案1】:

    http://www.w3schools.com/jsref/jsref_sort.asp

    使用 - 代替

    var value = CACHES.indexOf($(a).attr('id')) - CACHES.indexOf($(b).attr('id'));
    

    【讨论】:

    • 感谢您的回答和链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多