如何为不同的弹出窗口重用相同的jQuery ui对话框代码

时间:2012-09-18 10:22:13

标签: jquery jquery-ui

我想使用以下jQuery对话框打开不同的弹出框

<head runat="server">
    <title></title>
    <link href="css/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css" />
    <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="js/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#pop101").dialog({
                autoOpen: false,
                draggable: true,
                title: "Add New Person",
                open: function (type, data) {
                     $(this).parent().appendTo("form");
                }
            });
        });
        function showDialog(id) {
            $('#' + id).dialog("open");
        }
        function closeDialog(id) {
            $('#' + id).dialog("close");
        }
    </script>
</head>
<body>
    <input type="button" onclick="showDialog('pop101');" value="Popup1" />
    <input type="button" onclick="showDialog('pop102');" value="Popup2" />
    <input type="button" onclick="showDialog('pop103');" value="Popup3" />
    <input type="button" onclick="showpopup('pop104');" value="Popup4" />
    <div style="background: green" id='pop101'>
    </div>
    <div style="background: orange" id='pop102'>
    </div>
    <div style="background: blue" id='pop103'>
    </div>
    <div style="background: skyblue" id='dialog1'>
    </div>
</body>

popup1,2,3,4有4个按钮,当我点击它们时,弹出窗口应该用上面的代码显示,怎么样?

1 个答案:

答案 0 :(得分:1)

尝试使用类初始化弹出窗口:

尚未尝试但应该有效:

<head runat="server">
    <title></title>
    <link href="css/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css" />
    <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="js/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $(".popup").dialog({
                autoOpen: false,
                draggable: true,
                title: "Add New Person",
                open: function (type, data) {
                    $(this).parent().appendTo("form");
                }
            });
        });
        function showDialog(id) {
            $('#' + id).dialog("open");
        }
        function closeDialog(id) {
            $('#' + id).dialog("close");
        }
    </script>
</head>
<body>
    <input type="button" onclick="showDialog('pop101');" value="Popup1" />
    <input type="button" onclick="showDialog('pop102');" value="Popup2" />
    <input type="button" onclick="showDialog('pop103');" value="Popup3" />
    <input type="button" onclick="showpopup('pop104');" value="Popup4" />
    <div style="background: green" id='pop101' class="popup">
    </div>
    <div style="background: orange" id='pop102' class="popup">
    </div>
    <div style="background: blue" id='pop103' class="popup">
    </div>
    <div style="background: skyblue" id='dialog1' class="popup">
    </div>
</body>

Here is the fiddle; it worked!!