2009-09-11 53 views
2

此代码来自jQuery网站上的模式确认演示。只有当我点击一个按钮后,如何才能从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 --> 

任何人都可以告诉我只有当我点击一个按钮后才能使这个节目?现在它会在加载后自动显示在页面中。

回答

0

对或错,下面是一个通用的警报样式d ialog,我使用:

设置对话框(在文件准备好):

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

,打开对话框(某些事件发生后):

$("#generic-dialog") 
    .text("Status updated successfully.") 
    .dialog("option", "title", "Your Status").dialog("open"); 
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"/> 
+0

我只是*秒*的速度太慢... +1,我的答案被删除。 – 2009-09-11 20:47:31

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

这应该打开您已经定义对话框 - 附加对话框打开功能点击按钮

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

相关问题