Trigger event on dialog box open

无人久伴 提交于 2019-12-04 08:56:39

问题


My dialog box is defined under the div

#dialogbox

When the dialog box opens i want to trigger an event such that it alerts open. The code im using is:

$("#dialogbox").dialog({open: function(){
           alert("OPEN");
          }
});

But this doesnt seem to trigger when dialog box is opened Please help


回答1:


You can use this :

$( ".selector" ).dialog({
  open: function( event, ui ) {}
});

or the event listener .on

$( ".selector" ).on( "dialogopen", function( event, ui ) {} );

More information in this page :

http://api.jqueryui.com/dialog/#event-open




回答2:


Try this:

jsFiddle here

HTML:

<div id="dialogbox"></div>
<input id="mybutt" type="button" value="Click Me">

Javascript/jQuery:

$("#dialogbox").dialog({
    autoOpen:false,
    modal:true,
    title: "Use of Open event",
    width:300,
    open: function( event, ui ) {
        alert('hello');
    }
});

$('#mybutt').click(function() {
    $('#dialogbox').html('<h2>Watch this</h2>An alert box should have opened');
    $('#dialogbox').dialog('open');
});



回答3:


It will display alert after clicking on the OK button.

$( "#WaitingDialog").html("Message you want to display").dialog({
   modal: true,
   buttons: { 
    Ok: function() {
       alert("hello");
    }
}});

It will display alert after opening the modal

$( "#WaitingDialog").html("Message you want to display").dialog({
    modal: true,
    buttons: { 
        open: function( event, ui ) {
              alert('hello');
          }
    }});



回答4:


You can also use the focus event Click here for documentation



来源:https://stackoverflow.com/questions/18992081/trigger-event-on-dialog-box-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!