Javascript 使用getJSON方法的Jquery可排序列表

Javascript 使用getJSON方法的Jquery可排序列表,javascript,jquery-ui,getjson,Javascript,Jquery Ui,Getjson,我正在编写一个使用getJSON向DOM添加列表项的教程。还需要JqueryUI可排序插件来对列表进行排序。由于我不知道的原因,插件不能工作。我错过了什么?sortable函数是否应该位于getJSON回调中?任何建议都很好 以下是我迄今为止的代码: $(function () { $('body h1').append('My Todo List'); $.getJSON('todo.json', function(data) { var html = '<ul id="sort

我正在编写一个使用getJSON向DOM添加列表项的教程。还需要JqueryUI可排序插件来对列表进行排序。由于我不知道的原因,插件不能工作。我错过了什么?sortable函数是否应该位于getJSON回调中?任何建议都很好

以下是我迄今为止的代码:

$(function () {
 $('body h1').append('My Todo List');

 $.getJSON('todo.json', function(data) {

var html = '<ul id="sortable" class="ui-sortable">';

$.each(data, function(index) {

    var todo = data[index];
    if (todo.done === false) {
        todo.done = (" ")

    } else {
        todo.done = ("(DONE)")
    }
    html += '<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' + todo.who + " needs to " + todo.task + " by " + todo.dueDate + " " + todo.done + '</li>';
});
    html += '</ul>';
   $('body #container').append(html);
});



});
]

附加数据后,必须调用排序表。在$.getJSON回调中,再次调用排序表,如下所示。当DOM就绪时,只有当元素存在于DOM中时,jquery才会连接可排序表。您正在动态添加元素,因此在将元素添加到DOM中后,必须再次调用sortable

编辑

这是垃圾箱


演示使用内存中的数据,但即使在回调方法中也应该可以正常工作

是的,我已经尝试过这种方法,但这也不起作用。我已经在答案中添加了bin,其中包含sortable的工作演示和您提供的数据查看……查看更新的答案和工作jsbin演示。。。。。。。。。。。
<!DOCTYPE html>
<html>
<head>
    <title>Jquery ToDo List</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
    <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script src="todo.js"></script>
    <script>
        $(function () {
            $("#sortable").sortable("refresh");
            $("#sortable").disableSelection("refresh");
        });
    </script>
    <style>
        #sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
        #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 14px; height: 18px; }
        #sortable li span { position: absolute; margin-left: -1.3em; }
    </style>
</head>
<body>
<h1></h1>
<div id="container">

</div>



</body>
</html>
[
{"task":"get milk","who":"Scott","dueDate":"2013-05-19","done":false},
{"task":"get broccoli","who":"Elisabeth","dueDate":"2013-05-21","done":false},
{"task":"get garlic","who":"Trish","dueDate":"2013-05-30","done":false},
{"task":"get eggs","who":"Josh","dueDate":"2013-05-15","done":true}
 $.getJSON('todo.json', function(data) {

var html = '<ul id="sortable" class="ui-sortable">';

$.each(data, function(index) {

    var todo = data[index];
    if (todo.done === false) {
        todo.done = (" ")

    } else {
        todo.done = ("(DONE)")
    }
    html += '<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' + todo.who + " needs to " + todo.task + " by " + todo.dueDate + " " + todo.done + '</li>';
});
    html += '</ul>';
   $('body #container').append(html);
});

$( "#sortable" ).sortable();
$( "#sortable" ).disableSelection();

});