简体   繁体   English

jQuery Ajax计时

[英]JQuery Ajax Timing

I have a larger JQuery UI dialog. 我有一个更大的JQuery UI对话框。 Inside this I have some larger PHP Scripts, which fills the dialog content. 在其中,我有一些较大的PHP脚本,用于填充对话框内容。 On ESCAPE I close the dialog. 在ESCAPE上,关闭对话框。 After every: 每次之后:

$('.editbtn').click(function() {
  $id        = $(this).attr('name');
  var $tabid = $id.split("|");
  var $url   = "ajax/edit.php?....";

  $("#dialog-confirm").load($url);
  $("#dialog-confirm").dialog("option", "width",  $tabid[4]);
  $("#dialog-confirm").dialog("option", "height", $tabid[5]);
  $("#dialog-confirm").dialog('open');

  $("#dialog-confirm").first().focus();
});

I set the focus. 我确定了重点。

Sometimes it works somtimes not. 有时它有时无法工作。 It depends obviously on the execution of the PHP scripts. 显然,这取决于PHP脚本的执行。 I see the dialog with the old content (after close) and after a short gap of time the new selected content. 我看到带有旧内容的对话框(关闭后),不久后又看到了新的选定内容。 As long as the new selected content is not shown, ESCAPE works. 只要不显示新选择的内容,ESCAPE就会起作用。 if the dialog is filled with new content, ESCAPE doesn't work. 如果对话框中充满新内容,则ESCAPE不起作用。 This means in fact, the dialog screen lost the focus. 这实际上意味着对话框屏幕失去了焦点。

How can I make shure, that the dialog is only shown, if the PHP script ends? 如果PHP脚本结束,如何确保仅显示对话框?

Thanks in advance. 提前致谢。

You can simply do this with defining complete callback function. 您只需定义完整的回调函数即可完成此操作。

$('.editbtn').click(function() {
  $id        = $(this).attr('name');
  var $tabid = $id.split("|");
  var $url   = "ajax/edit.php?....";

  $("#dialog-confirm").load($url, function() {
    $("#dialog-confirm").dialog("option", "width",  $tabid[4]);
    $("#dialog-confirm").dialog("option", "height", $tabid[5]);
    $("#dialog-confirm").dialog('open');

    $("#dialog-confirm").first().focus();
  });
});

And, I would suggest you to define a variable for the jQuery object for better performance. 而且,我建议您为jQuery对象定义一个变量,以提高性能。

var confirm = $("#dialog-confirm");

confirm.dialog("option", "width",  $tabid[4]);
confirm.dialog("option", "height", $tabid[5]);
confirm.dialog('open');

confirm.first().focus();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM