jQuery UI Draggable/Sortable - Dynamically added items not draggable

前端 未结 1 1088
逝去的感伤
逝去的感伤 2021-02-05 22:22

I have a Draggable/Sortable set of lists that I am dynamically adding items to, but the trouble is, that once an item has been added, it\'s not possible to move them to the new

相关标签:
1条回答
  • 2021-02-05 22:50

    UPDATED ANSWER

    You have to call .draggable() for each element that gets added. The jQuery selector that you use at initialization time only applies to elements that exist at the moment, not to the ones you will create.

    Here's some JS that should work:

    var draggable_opts = {
                connectToSortable: ".sph-callout-portlet",
                helper: "clone",
                opacity: 0.75,
                revert: 'invalid',
                stop: function(event, ui) {
                    //alert(ui)
                }
            };
    
    $(function() {
        $( ".sph-callout-portlet" ).sortable({
            opacity: 0.75,
            placeholder: "ui-state-highlight",
        }).disableSelection();
    
        $( "#sph-callout-portlet-avail li" ).draggable(draggable_opts);
    
        $(document).on("click",'#addNewCo',function(e){
            e.preventDefault();
            var newCo = $('<li>New Callout</li>').draggable(draggable_opts);
            $('#sph-callout-portlet-avail').append(newCo);
        });
    });​
    

    http://jsfiddle.net/SGevw/

    0 讨论(0)
提交回复
热议问题