0

我在使用 jquery 触发函数传递额外参数时遇到问题。额外的参数没有通过。代码 :

//click event binding with button goes here
$("#b").bind({ click: 
   function (e, x, y) { 
   alert(x + " " + y);   //here`s the problem, showing undefined (expecting top & left  
}                        //offset() of dropped button 
});

//here making the button draggable for drag drop functionality (applying jquery-ui)
$("#b").draggable({ helper : "clone" });

//here creating a droppable area ( here i am dropping a draggable button)
$(".droppable").live({ mouseenter: function () {
    $(this).droppable({
        accept: "#b",              // accepting only button whose id = b
        drop: function (ev, ui) {
            alert("dropped");      //alert is showing means droppable works fine  
            var y = ui.offset.top;
            var x = ui.offset.left;
            ui.draggable.trigger("click", [x , y]); // here i am triggering the click event
        }                                           // for button and passing x and y as an     
    });                                             // extra parameter 
}
});
4

1 回答 1

1

我绘制了这种情况,它对我来说很好(使用相同的代码)。

什么是可拖放的元素?我的是:

    <div id="b" style="width:200px;height:200px;background-color:red"></div>

    <div class="droppable" style="width:400px;height:200px;background-color:green"></div>

我的建议:

在获得偏移值后添加console.log(x, y)- 可拖动元素有可能没有它们(并且值未定义)

通过以下方式更改您的点击处理程序,以便您可以处理用户点击和触发点击:

function (e, x, y) { 
                if(typeof x !== "undefined" && typeof y !== undefined) {
                    // do trigger logic
                    return;
                }
                // do click logic
            }
于 2012-09-14T22:59:31.503 回答