dongwei3151 2016-06-06 09:35
浏览 20
已采纳

JQuery Ajax时序

I have a larger JQuery UI dialog. Inside this I have some larger PHP Scripts, which fills the dialog content. On ESCAPE I close the dialog. 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. 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. if the dialog is filled with new content, ESCAPE doesn't work. 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?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duanjiao3686 2016-06-06 11:00
    关注

    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.

    var confirm = $("#dialog-confirm");
    
    confirm.dialog("option", "width",  $tabid[4]);
    confirm.dialog("option", "height", $tabid[5]);
    confirm.dialog('open');
    
    confirm.first().focus();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码