只有在按下按钮后,如何才能从jQuery show中创建一个对话框?

时间:2009-09-11 20:39:49

标签: jquery dialog

此代码来自jQuery网站上的模态确认演示。

<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 -->

任何人都可以告诉我如何在按下按钮后才能制作此节目吗?现在它在加载后立即自动显示在页面中。

3 个答案:

答案 0 :(得分:2)

将按钮添加到页面并将代码添加到按钮的单击事件中。

<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>

和HTML

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

答案 1 :(得分:1)

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

那应该打开你定义的对话框 - 附加对话框打开功能来点击按钮事件

哦 - 并在对话框设置属性上设置autoOpen: false:)

答案 2 :(得分:0)

对或错,这是我使用的通用警报样式对话框的示例:

设置对话框(文档就绪):

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

打开对话框(在某些事件之后):

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