How do I trigger the Drop event with jQuery UI Droppable without actually dragging and dropping?

前端 未结 3 1807
礼貌的吻别
礼貌的吻别 2020-12-10 03:26

I have a droppable with a drop event handler:

$(this).droppable({
  drop:function(){
    console.log(\'OMG You Dropped It!\');
  }
});


        
相关标签:
3条回答
  • 2020-12-10 03:56

    You can trigger the function associated with the drop call via the option-method:

    $("#droppable").droppable({
            drop: function(event, ui) {
                do stuff }
        });
    var drop_function = $("#droppable").droppable.option('drop');
    drop_function();
    

    This way you get whatever would happen when dropping something on droppable. Of course you could just execute the function instead of assigning it. It's nonetheless a good idea to assign a function to drop, that you define somewhere else, just for clarities sake.

    0 讨论(0)
  • 2020-12-10 03:56

    As pointed by StuperUser and based on ajmurmann's answer, with the recent versions of jQuery you should do:

    $("#droppable").droppable({
        drop: function(event, ui) {
            do stuff }
    });
    var drop_function = $("#droppable").droppable('option', 'drop');
    drop_function();
    
    0 讨论(0)
  • 2020-12-10 04:10

    You should move the code in your drop handler to a separate function.
    You can then call the function both in the handler and elsewhere.

    0 讨论(0)
提交回复
热议问题