创建对话框后的jQuery ui对话框触发事件

时间:2014-11-05 17:48:19

标签: javascript jquery jquery-ui dialog

我试图在创建jQuery ui对话框后触发函数setupCheckBox('chk-img');,但我无法破解它。任何帮助表示赞赏!

我已尝试打开和创建事件(我在代码的开头绑定):

jQuery("#TicketTC").on( "dialogcreate", function( event, ui ) {
    setupCheckBox('chk-img');
});

jQuery("#TicketTC").on( "dialogopen", function( event, ui ) {
    setupCheckBox('chk-img');
});

我的对话框代码是:

jQuery("#TicketTC").dialog({
    modal: true,
    width: 600,
    height: 'auto',
    autoOpen: true,
    buttons: {
        CONTINUE: function() {
            jQuery(this).dialog("close");
            return true;
        },
        CANCEL: function() {
            jQuery(this).dialog("close");
            return false;
        }
    }
}).html('<div class="support-msg">' + tempHTML + '</div>');

2 个答案:

答案 0 :(得分:1)

您应该在初始化对话框之前绑定事件处理程序,因为默认情况下对话框设置为打开(如果之后绑定事件,则不会调用事件,因为两个事件已经发生

不是手动绑定事件,而是使用回调初始化窗口小部件将是更安全的方法:

jQuery("#TicketTC").on("dialogcreate", function (event, ui) {
   setupCheckBox('chk-img');
});
jQuery("#TicketTC").on("dialogopen", function (event, ui) {
   setupCheckBox('chk-img');
});
jQuery("#TicketTC").dialog({
   modal: true,
   width: 200,
   height: 'auto',
   autoOpen: true,
   create: function (event, ui) { // this is more reliable
      setupCheckBox('chk-img');
   },
   buttons: {
    CONTINUE: function () {
        jQuery(this).dialog("close");
        return true;
    },
    CANCEL: function () {
        jQuery(this).dialog("close");
        return false;
    }
  }
}).html('<div class="support-msg"></div>');

JSFiddle

答案 1 :(得分:1)

dialog(...)立即打开对话框。因此,之后使用html设置的html不在对话框中。并绑定到dialogopen事件。

jQuery("#TicketTC")
  .on("dialogopen", function (event, ui) {
    setupCheckBox('chk-img');
  })
  .html('<div class="support-msg"></div>')
  .dialog({
    modal: true,
    width: 200,
    height: 'auto',
    autoOpen: true,
    buttons: {
      CONTINUE: function () {
        jQuery(this).dialog("close");
        return true;
      },
      CANCEL: function () {
        jQuery(this).dialog("close");
        return false;
      }
    }
  });