如何在可拖动停止时获取目标/类名称

时间:2018-03-29 02:03:19

标签: javascript jquery jquery-ui jquery-ui-draggable

我使用Jquery UI draggable, 我想在可拖动列表菜单的时候在喜欢的菜单上添加菜单... 我使用选项appendTo,但问题是,当我拖动到收藏的容器时菜单如何添加到收藏菜单...

抱歉我的英语不好......

enter image description here

2 个答案:

答案 0 :(得分:0)

$("YourSelector").draggable(
        {
            start: function(event, ui)
            {
                //This Occurs when drag start
                selectedClass = $(this).attr('data-value');
                console.log(selectedValue);
            },
            stop: function() { 
                //This Occurs when drag stop
                selectedClass = $(this).attr('data-value');
                console.log(selectedValue);}
        });

答案 1 :(得分:0)

$(".navbar-menu").draggable({
            appendTo: ".navbar-favoritebody ul",
            axis: "y",
            containment: ".navbar-left",
            handle: "a:not([data-disabled]):not([data-type='favorite']):not(:contains('Dashboard'))",
            helper: "clone",
            start: function(ev, ui) {
                $("body").prepend("<div class=\"ondrag\"></div>");
                var _this = $(this);
                _this.find("a").attr("data-href","favorite/create");
            },
            drag: function(ev, ui) {},
            stop: function(ev, ui) {
                $(".ondrag").remove();
                var _this = $(this),
                    desc = parsing.toUpper(_this.find("a").attr("data-slug").replace(/\-/g, " "), 2);

                var target = $(event.target);
                console.log(target.prop('className'));
                if(target.prop("className") == "navbar-favoritebody") {
                    $.ajax({
                        type: "POST",
                        url: location.pathname+"?r="+_this.find("a").attr("data-href"),
                        data: {
                         "Favorite[menu_id]": _this.find("a").attr("data-menu"),
                        },
                        dataType: "text",
                        error: function(xhr, errors, message) {
                        console.log(xhr, errors, message);
                        },
                        beforeSend: function() {
                        navigasi.process("load");
                        },
                        success: function(data) {
                            if(!$.trim(data)) {
                                notification.open("danger", "Menu "+ desc +" sudah ada di Favorite.", 2000);
                            } else {
                                $(".navbar-favoritebody").html(data);
                                notification.open("success", "Menu "+ desc +" berhasil ditambah ke Favorite.", 2000);
                            }
                        },
                        complete: function() {
                        navigasi.process("destroy");
                            _this.find("a").removeAttr("data-href");
                         }
                     });
                 }
            },