【问题标题】:jQuery Draggable only allowing 1 image to be draggablejQuery Draggable 仅允许 1 个图像可拖动
【发布时间】:2011-08-26 10:43:37
【问题描述】:

我有一页完整的图片,准确地说是专辑封面。我添加了代码使其可拖动,它只允许我拖动页面上的第一张图片。

这是为什么?

 $(document).ready(function () {
            $("#draggable").each(function () {
                $(this).draggable({
                    revert: function (dropped) {
                        var dropped = dropped && dropped[0].id == "droppable";
                        if (!dropped);
                        return !dropped;
                    }
                });
            });

            $("#droppable").droppable({
                drop: function (event, ui) {
                    var AlbumToAdd = ui.draggable.data("id");
                    if (AlbumToAdd != '') {
                        // Perform the ajax post
                        $.post("/ShoppingCart/DragToCart", { "id": AlbumToAdd },
                            function (data) {
                                // Successful requests get here
                                // Update the page elements
                                $('#cart-total').text(data.CartTotal);
                            });
                    }
                }
            });

        });

如果您需要查看更多代码,请发表评论

【问题讨论】:

    标签: javascript jquery-ui drag-and-drop


    【解决方案1】:

    通常 id 应该是唯一的。所以做$("#draggable").each... 通常不是一个好习惯。将其更改为类选择器应该可以解决问题。

    所以把它变成:

    $(".draggable").each(function () { ...
    

    和你的html成

    <div class="draggable" class="ui-widget-content">
        <p>Drag me around</p>
    </div>
    

    您可以查看我的工作示例(简化)here。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      相关资源
      最近更新 更多