【问题标题】:How can I call function in drop method of jquery?如何在 jquery 的 drop 方法中调用函数?
【发布时间】:2013-12-05 07:07:37
【问题描述】:

我在 jquery 中有一些带有可拖放元素的代码。

这是我的 jsfiddle 链接 - http://jsfiddle.net/hirenwebdp/Mf6zJ/333/

我想通过为

创建一个函数来优化这段代码
drop: function(event, ui) {
        var self = $(this);
        self.find(".placeholder").remove();
        var productid = ui.draggable.attr("data-id");
        if (self.find("[data-id=" + productid + "]").length) return;
        $("<li></li>", {
            "text": ui.draggable.text(),
            "data-id": productid
        }).appendTo(this);
             $("#equal").hide();

        // To remove item from other shopping chart do this
        var cartid = self.closest('.shoppingCart').attr('id');
        $(".shoppingCart:not(#"+cartid+") [data-id="+productid+"]").remove();
           var isAllFilled = true;
        $(".shoppingCart").each(function(){
            if($(this).find('ol .placeholder').length > 0)
            {
                isAllFilled = false;
                return
            }
                   });
        if(isAllFilled)
        {
            $("#equal").show();
             $("#equal").html("Congratulation! You have entered each block in correct place.");
        }

所以我编写了代码但无法正常工作。元素被拖动但不可放置。

优化后的jsfiddle链接为http://jsfiddle.net/Mf6zJ/486/

那么如何解决这个问题。 请先检查两个 jsfiddle 链接,以便你们了解我面临的问题。

请帮忙。

【问题讨论】:

    标签: javascript jquery performance jquery-ui


    【解决方案1】:

    在您的第二个版本中传递eventui 参数

    function test(event, ui){ ...
    

    因此,您需要在函数内将 elemnt 引用为 this,像这样调用 test 函数

    drop: function(event, ui) {
    
        test.call(this, event, ui);
    
    }
    

    DEMO

    【讨论】:

    • @hirenpanchal 有什么问题吗?
    • 不是call.test(this,event, ui);,而是test.call(this,event, ui); :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    相关资源
    最近更新 更多