2

我正在使用 jQuery sortable + jQuery draggable 将添加和删除图像到幻灯片的页面放在一起。我现在已经设置好了,这样我就可以拖入新屏幕并将它们放入我想要的命名序列中,它会创建我需要的 DOM 元素。

我正在成功扫描页面以查找必要的元素并将它们提交到数组中,但它永远不会拾取新添加的项目。例如,我有以下元素开始:

<h3 data-sequence-title="sequence1">Sequence 1</h3>
<ul class="sortable connectedSortable ui-sortable">
    <li data-screen="screen1">Screen 1</li>
    <li data-screen="screen2">Screen 2</li>
    <li data-screen="screen3">Screen 3</li>
</ul>

...然后我添加一行:

<h3 data-sequence-title="sequence1">Sequence 1</h3>
<ul class="sortable connectedSortable ui-sortable">
    <li data-screen="screen1">Screen 1</li>
    <li data-screen="screen2">Screen 2</li>
    <li data-screen="screen3">Screen 3</li>
    <li data-screen="screen4">Screen 4</li>
</ul>

jQuery 只会返回第一个元素,而不是更新的元素。这是我正在使用的js:

$( document.body ).on('click', '.submit', function(){

    // Build nested array from DOM elements
    var jsonObj = [];    

    $('.sortable').prev('h3').each(function(){
        var obj = {
            title: $(this).data("sequence-title"),
            Screens: []
        };

        $(this).next("ul").children('li').each(function() {
          obj.Screens.push({
              image: $(this).data("screen")
          });
        });

        jsonObj.push(obj);
    });

});

添加新 LI 的代码:

// Initialize draggable / droppable functionality
$('.sortable').sortable({
    placeholder: 'ui-state-highlight',
    revert: true
});
$('.draggable li').draggable({
    connectToSortable: '.sortable',
    helper: 'clone',
    revert: 'invalid'
});
$('.sortable').disableSelection();

我需要返回修改后的 DOM,而不是加载页面时的元素。

4

2 回答 2

0

将元素添加到可排序后,您必须更新组件:

$(".sortable").sortable("refresh");
于 2013-05-21T16:45:02.190 回答
0

我根据您的代码做了一个快速实验。我可以从下面拖动元素并将它们添加到第一个序列中。然后在单击 clicky 按钮时可以访问它们。

  <html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.10.3.custom.min.js"></script>
</head>
<body>
<div id="moo">clicky</div>

<ul class="sortable connectedSortable ui-sortable">
    <h3 data-sequence-title="sequence1">Sequence 1</h3>
    <li data-screen="s1screen1">Seq 1 Screen 1</li>
    <li data-screen="s1screen2">Seq 1 Screen 2</li>
    <li data-screen="s1screen3">Seq 1 Screen 3</li>
</ul>



<ul class="draggable">
    <h3 data-sequence-title="sequence2">Dragable Sequence (Drag these elements up and assign to sequence 1)</h3>
    <li data-screen="s2screen1">Seq 2 Screen 1</li>
    <li data-screen="s2screen2">Seq 2 Screen 2</li>
    <li data-screen="s2screen3">Seq 2 Screen 3</li>
</ul>


<script>
$(document.getElementById('moo')).on('click', function(){

    // Build nested array from DOM elements
    var jsonObj = [];    

    $('.sortable h3').each(function(){
        var obj = {
            title: $(this).data("sequence-title"),
            Screens: []
        };

        $(this).siblings("li").each(function() {
          obj.Screens.push({
              image: $(this).data("screen")
          });
        });

        jsonObj.push(obj);
    });

    alert(JSON.stringify(jsonObj));
});

// Initialize draggable / droppable functionality
$('.sortable').sortable({
    placeholder: 'ui-state-highlight',
    revert: true
});
$('.draggable li').draggable({
    connectToSortable: '.sortable',
    helper: 'clone',
    revert: 'invalid'
});
$('.sortable').disableSelection();

</script>

</body>
</html>
于 2013-05-21T16:39:18.513 回答