javascript - jQuery live and sortable - Stack Overflow

I have the following static html:<ul id="mylist"><li id="li_10"><a h

I have the following static html:

<ul id="mylist">
    <li id="li_10"><a href="10">Item 10</a></li>
    <li id="li_20"><a href="20">Item 20</a></li>
    <li id="li_30"><a href="30">Item 30</a></li>
    <li id="li_40"><a href="40">Item 40</a></li>
    <li id="li_50"><a href="50">Item 50</a></li>
</ul>

I have the following jQuery:

<script>
    $( document ).ready( function() { 
        $("#mylist").sortable(
            {axis:"y"}
        );
    });
</script>

This works perfectly, but stops working as soon as I use jQuery/AJAX to generate the above HTML. So I am assuming I need to use the "live" function in jQuery to do the sortable section. Can someone help me implement this?

I have the following static html:

<ul id="mylist">
    <li id="li_10"><a href="10">Item 10</a></li>
    <li id="li_20"><a href="20">Item 20</a></li>
    <li id="li_30"><a href="30">Item 30</a></li>
    <li id="li_40"><a href="40">Item 40</a></li>
    <li id="li_50"><a href="50">Item 50</a></li>
</ul>

I have the following jQuery:

<script>
    $( document ).ready( function() { 
        $("#mylist").sortable(
            {axis:"y"}
        );
    });
</script>

This works perfectly, but stops working as soon as I use jQuery/AJAX to generate the above HTML. So I am assuming I need to use the "live" function in jQuery to do the sortable section. Can someone help me implement this?

Share Improve this question asked Dec 1, 2010 at 11:03 oshirowanenoshirowanen 16k83 gold badges205 silver badges357 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

.live() is event based, so you can't use it for plugins like this. What you can easily do is call that code when your AJAX call finishes, for example:

$.ajax({
 //options...
  success: function(data) {
    //create UL
    $("#mylist").sortable({axis:"y"});
  }
});

The same goes for short forms of $.ajax(), for example:

$("#mylist").load("pageThatGivesTheLIElementGoodness.htm", function() {
  $(this).sortable({axis:"y"});
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744773477a4592903.html

相关推荐

  • javascript - jQuery live and sortable - Stack Overflow

    I have the following static html:<ul id="mylist"><li id="li_10"><a h

    15小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信