Model form (event)

Model form (event)

Hi all, 
I'm working on modal form, and I have a problem with event.  The application recognize them on the second click.  
The form is loading when the application call the action through url (JAVA).
Here my code

document = {
    init: function(url) {
var dialog = $('<div id="loading"><div id="alert-load">Loading...</div></div>')
dialog.load(url);
dialog.dialog({
autoOpen: false,
title: title,
width: 750,
height: 500
});
dialog.dialog('open');
$("loading").hide();
},
display: function(url,title, method, idForm){
$("#"+idForm).submit(function(){
$.ajax({
 type: method,
 url: url,
 data: $('.ui-dialog-content form').serialize(),
 async: true,
 error: function(){
$(".ui-dialog-content").append("error");

    },
  success:function (data) {
    $(".ui-dialog-content").text("");
    $(".ui-dialog-content").append(data);
   
}
   
 
}); 
});
},
}
Thanks for you help.
Celi