拖放删除克隆

发布于 2024-11-27 20:27:13 字数 2555 浏览 1 评论 0原文

我正在将 jquery Draggables 与 Sortable 一起使用。我在左侧有一个元素列表,需要将其拖放到右侧的不同 div 中。可以将左侧相同的项目放入多个框中,因此我使用了助手:“克隆”。一切正常,直到我需要获取对接收事件中克隆的新元素的引用,以便我可以向其添加单击处理程序。

我需要新元素的引用!

<div>
        <div style="float: left; width: 300px;">
            <div id="FieldList">
                <h2>Loading</h2>            
            </div>
            <input type="button" name="cmdTest" id="cmdTest" value="Test" />
    </div>
    <div style="float: right; width: 300px;">
        <div>
            <div>Select Columns</div>
            <div class="Toolbar"></div>
            <div id="SelectFields" class="DroppableField">

            </div>
        </div>
        <div>
            <div>Conditions</div>
            <div class="Toolbar"></div>
            <div class="DroppableField" style="background-color: Gray; height: 200px;">

            </div>
        </div>
        <div>
            <div>Order</div>
            <div class="Toolbar"></div>
            <div class="DroppableField" style="background-color: Gray; height: 200px;">

            </div>
        </div>
    </div>
    <div style="clear: both;">
    </div>
</div>

Javascript:

$(document).ready(function () {

        $(".DroppableField").sortable({
            revert: true,
            receive: function (ui, event) { 
                // ui.helper always seems to be null. Many of the other properties seem to reference the original item.  I need the NEW ONE!!!                  
                $(ui.helper).dblclick(function () {

                });
            }
        });

        $.ajax({
            url: "/condition/getconditions",
            data: {},
            dateType: "json",
            type: "POST",
            success: function (data, successcode, jqXHR) {
                var html = "";
                for (var i = 0; i < data.length; i++) {
                    $("#FieldList").append("<div>" + data[i].Description + "</div>").find("div:last").data("fielddata", data[i]);
                    //html += ;
                }

                $("#FieldList div").draggable({
                    connectToSortable: ".DroppableField",
                    helper: "clone",
                    revert: "invalid"
                });
            }
        });

    });

I'm using jquery Draggables with a Sortable. I have a list of elements on the left that I need to drag and drop into different divs on the right. It's possible to put the the same items from the left into multiple boxes, so I used helper: "clone." All works well until I need to get a reference to the new element that was cloned in the receive event so I can add a click handler to it.

I need a reference to the new element!

<div>
        <div style="float: left; width: 300px;">
            <div id="FieldList">
                <h2>Loading</h2>            
            </div>
            <input type="button" name="cmdTest" id="cmdTest" value="Test" />
    </div>
    <div style="float: right; width: 300px;">
        <div>
            <div>Select Columns</div>
            <div class="Toolbar"></div>
            <div id="SelectFields" class="DroppableField">

            </div>
        </div>
        <div>
            <div>Conditions</div>
            <div class="Toolbar"></div>
            <div class="DroppableField" style="background-color: Gray; height: 200px;">

            </div>
        </div>
        <div>
            <div>Order</div>
            <div class="Toolbar"></div>
            <div class="DroppableField" style="background-color: Gray; height: 200px;">

            </div>
        </div>
    </div>
    <div style="clear: both;">
    </div>
</div>

Javascript:

$(document).ready(function () {

        $(".DroppableField").sortable({
            revert: true,
            receive: function (ui, event) { 
                // ui.helper always seems to be null. Many of the other properties seem to reference the original item.  I need the NEW ONE!!!                  
                $(ui.helper).dblclick(function () {

                });
            }
        });

        $.ajax({
            url: "/condition/getconditions",
            data: {},
            dateType: "json",
            type: "POST",
            success: function (data, successcode, jqXHR) {
                var html = "";
                for (var i = 0; i < data.length; i++) {
                    $("#FieldList").append("<div>" + data[i].Description + "</div>").find("div:last").data("fielddata", data[i]);
                    //html += ;
                }

                $("#FieldList div").draggable({
                    connectToSortable: ".DroppableField",
                    helper: "clone",
                    revert: "invalid"
                });
            }
        });

    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花海 2024-12-04 20:27:13

您似乎颠倒了回调函数接收的两个参数。

receive: function (ui, event) { 

}

应该

receive: function (event, ui) { 

}

根据 jQuery UI 文档

所有回调都接收两个参数:原始浏览器事件和
准备好的 ui 对象[...]

You seem to have inverted the two parameters the callback functions receives.

receive: function (ui, event) { 

}

Should be

receive: function (event, ui) { 

}

According to the jQuery UI Docs:

All callbacks receive two arguments: The original browser event and a
prepared ui object[...]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文