5

我想通过在按钮和内容 div 上使用相同的类来打开多个对话框。下面的作品,但只是第一次。

jQuery('.helpDialog').hide();

jQuery('.helpButton').click(function() {  
    jQuery(this).next('.helpDialog').dialog({  
    autoOpen: true,  
    title: 'Help',  
    width: 500,  
    height: 300,  
    position: [180,10],  
    draggable: true,    
    resizable: false,  
    modal: false  
    });  
return false;  
});  

我们知道这个http://blog.nemikor.com/2009/04/08/basic-usage-of-the-jquery-ui-dialog/的原因 “第二次调用被忽略,因为对话框已经被实例化了那个元素。”

但是当我通过尝试下面的代码来解决这个问题时,对话框不再打开。任何人都可以帮忙吗?提前致谢

jQuery('.helpDialog').hide();

jQuery(function() {  
    jQuery('.helpDialog').dialog({  
        autoOpen: false,  
        modal: true,  
        title: 'Info',  
        width: 600,  
        height: 400,  
        position: [200,0],  
        draggable: false  
    });  
});  

jQuery('.helpButton').click(function() {  
    jQuery(this).next('.helpDialog').dialog('open');  
    return false;  
});  
4

3 回答 3

21

您实际上需要一种不同的方法,一种非直观的方法,如下所示:

jQuery(function($) {
  $('.helpButton').each(function() {  
    $.data(this, 'dialog', 
      $(this).next('.helpDialog').dialog({
        autoOpen: false,  
        modal: true,  
        title: 'Info',  
        width: 600,  
        height: 400,  
        position: [200,0],  
        draggable: false  
      })
    );  
  }).click(function() {  
      $.data(this, 'dialog').dialog('open');  
      return false;  
  });  
});  

你可以在这里测试一下

为什么必须这样做?因为.dialog()将它包含在对话框元素中的内容移动到 的末尾<body>,所以.next()将不再找到它...通过使用jQuery.data()我们维护对我们要打开的对话框的引用。

于 2010-12-23T12:53:56.533 回答
2

也许这段代码会对你有所帮助。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="stylesheet" href="Styles/ui-lightness/jquery-ui-1.7.2.custom.css" />

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {

        $("#opener1").click(function () {
            $("<div id='dialog1' />").dialog(
            {
                title: 'Basic modal dialog1',
                autoOpen: false,
                width: 600,
                height: 400,
                open: function (event, ui) {
                }
            });

            $("#dialog1").dialog('open').show();
            return false;
        });


        $("#opener2").click(function () {
            $("<div id='dialog2' />").dialog(
            {
                title: 'Basic modal dialog2',
                autoOpen: false,
                width: 600,
                height: 400,
                open: function (event, ui) {
                }
            });

            $("#dialog2").dialog('open').show();
            return false;
        });

    });
</script>

</head>
<body>
    <form id="form1" runat="server">
    <button id="opener1">open the dialog1</button>
    <button id="opener2">open the dialog2</button>

    </form>
</body>
</html>
于 2014-07-02T14:48:29.893 回答
-1
jQuery(function($) {
$('.helpButton').each(function() {  
$.data(this, 'dialog', 
$(this).next('.helpDialog').dialog({
autoOpen: false,  
modal: true,  
title: 'Info',  
width: 600,  
height: 400,  
position: [200,0],  
draggable: false  
})
);  
}).click(function() {  
$.data(this, 'dialog').dialog('open');  
return false;  
});  
});
于 2016-08-16T06:11:13.023 回答