How can I make the dialog from jQuery show only after I clicked the button?

This code is from the jQuery site modal validation demo.

<script type="text/javascript">
$(function() {
    $("#dialog").dialog({
        bgiframe: true,
        resizable: false,
        height:140,
        modal: true,
        overlay: {
            backgroundColor: '#000',
            opacity: 0.5
        },
        buttons: {
            'Yes': function() {
                $(this).dialog('close');
            },
            'No': function() {
                $(this).dialog('close');
            }
        }
    });
});
</script>



<div class="demo">

<div id="dialog" title="Empty the recycle bin?">
    <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>

<!-- Sample page content to illustrate the layering of the dialog -->
<div class="hiddenInViewSource" style="padding:20px;">
    <p>Sed vel diam id libero <a href="http://example.com">rutrum convallis</a>. Donec aliquet leo vel magna. Phasellus rhoncus faucibus ante. Etiam bibendum, enim faucibus aliquet rhoncus, arcu felis ultricies neque, sit amet auctor elit eros a lectus.</p>
    <form>
        <input value="text input" /><br />
        <input type="checkbox" />checkbox<br />
        <input type="radio" />radio<br />
        <select>
            <option>select</option>
        </select><br /><br />
        <textarea>textarea</textarea><br />
    </form>
</div><!-- End sample page content -->

</div><!-- End demo -->

<div class="demo-description">

<p>Confirm an action that may be destructive or important.  Set the <code>modal</code> option to true, and specify primary and secondary user actions with the <code>buttons</code> option.</p>

</div><!-- End demo-description -->

      

Can anyone tell me how to make this show only after I press the button? Right now, it is automatically displayed on the page immediately upon loading.

+2


source to share


3 answers


Right or wrong, here's an example from the generic alert style dialog I'm using:

Set up the dialogue (in the finished document):

$("#generic-dialog").dialog({
    autoOpen: false,
    buttons: {
        Ok: function() {
            $(this).dialog("close");
        }
    }
});

      



And open a dialog (after some event):

$("#generic-dialog")
   .text("Status updated successfully.")
   .dialog("option", "title", "Your Status").dialog("open");

      

0


source


Add a button to the page and add code to the button click event.

<script type="text/javascript">
$(function() {
    $("#button").click(function(){
    $("#dialog").dialog({
            bgiframe: true,
            resizable: false,
            height:140,
            modal: true,
            overlay: {
                    backgroundColor: '#000',
                    opacity: 0.5
            },
            buttons: {
                    'Yes': function() {
                            $(this).dialog('close');
                    },
                    'No': function() {
                            $(this).dialog('close');
                    }
            }
      });
   });
  });
</script>

      



and in HTML

<input type='button' value="click" id="button"/>

      

+2


source


$("#btnTest").click(function() {
       $('#dialog').dialog('open');
        return false;
    })

      

This should open the dialog you defined - add the dialog open function to click the button event

Oh - and set autoOpen: false

in the properties of the dialog settings :)

+1


source







All Articles