0

我有很多对话框,我需要一个简单的关闭按钮解决方案。我真的不想编写大量代码来关闭每个代码。

我想对要关闭对话框的所有按钮和链接使用 class="btnDone"。除了为每个对话框中每个按钮的每个实例编写单独的函数之外,还有更简单的方法吗?

以下是其中一个对话框代码的示例:

    <script>
    // increase the default animation speed to exaggerate the effect
    $.fx.speeds._default = 1000;
    $(function() {
        $( "#forgotPassword" ).dialog({position:['middle',60],
            open: function(event, ui) {  
            jQuery('.ui-dialog-titlebar-close').removeClass("ui-dialog-titlebar-close").html('<span style="float:right;"><img src="../images/x.png" /></span>');  
        },  
            dialogClass: 'ui-widget-shadow',
            modal: true,    
            autoOpen: false,
            width: '650px',
            close: function(ev, ui) {$(this).close();}
        });

        $( ".forgotPasswordOpen" ).click(function() {
            $( "#forgotPassword" ).dialog( "open" );
            return false;
        });
    });
    </script>
<div style="display:none">
    <div id="forgotPassword">
        <!--#include file="modal08.asp"-->
    </div>
</div>

如何使用 class="btnDone" 编写关闭函数?

4

3 回答 3

5

你看过API吗?就像打开一个

$( ".btnDone" ).click(function(){
  $('.ui-dialog-content').dialog( "close" );
})
于 2013-01-11T15:50:51.993 回答
1

我认为您正在尝试拥有一个关闭所有对话框的主按钮。如果每个对话框都包含一个样式为 的按钮.btnDone,例如:

<div>
My great dialog
<button class="btnDone">Get outta here</button>
</div>

<div>
Another thought
<button class="btnDone">Close</button>
</div>

<div>
Check this out
<button class="btnDone">Away!</button>
</div>

您可以使用$('.btnDone').parent('div').dialog('close');关闭或$('.btnDone').parent('div').hide();隐藏所有对话框。

有关工作示例,请参见http://jsfiddle.net/jhfrench/PHcL4/1/ 。

于 2013-01-11T15:50:30.337 回答
1
<script>
   $( ".btnDone" ).click(function() {
       $(this).parent().dialog( "close" );
   });
</script>
<div id="forgotPassword">
   <input type="button" class="btnDone" value="Done" />
</div>
<div id="forgotUsername">
   <input type="button" class="btnDone" value="Done" />
</div>

假设按钮在对话框中,您需要做的就是调用按钮的 .parent() 以引用该特定对话框。

不过,我之前没有使用过 jQuery UI,所以不知道“关闭”是否是您传递给对话框以关闭它的内容。

于 2013-01-11T15:51:32.350 回答