开发者

Why does my modal jQuery dialog open multiple times?

开发者 https://www.devze.com 2023-03-08 10:24 出处:网络
I want to use jQuery dialog to open answer form in a modal dialog. After loading page, for the first time, it\'s ok, but after that by each click it will open 2^n-1 times!!! (n is count of clicks)

I want to use jQuery dialog to open answer form in a modal dialog. After loading page, for the first time, it's ok, but after that by each click it will open 2^n-1 times!!! (n is count of clicks)

something like this:

click-> open dialog (1 time) -> close dialog

click-> open dialog (2 times) -> close dialog

click-> open dialog (4 times) -> close dialog

click-> open dialog (8 times) -> close dialog

this is the code:

$(function () {
    $('label.answer').click(function (event) { openInDialog(this, event, 'http://localhost/Questions/Answer/2') });
});

function openInDialog(element, event, target) 
{
    event.preventDefault();
    var $loading = $('<img src="../../Others/images/ajaxLoading.gif" alt="loading" class="ui-loading-icon">');
    var $url = target;    
    var $title = "Title"; 
    var $dialog = $('<div></div>');
    $dialog.empty();


    $dialog
            .append($loading)
            .load($url)
            .dialog(
            {
          开发者_开发百科      autoOpen: false
                , title: $title
                , modal: true
                , show: 'fade'
                , hide: 'fade'
            });

    $dialog.dialog('open');
};


initialize the dialog outside of the function. You should also not try to open the dialog until the success of the load.

$(function () {
    $('label.answer').click(function (event) { openInDialog(this, event, 'http://localhost/Questions/Answer/2') });
});
var $dialog = $('<div></div>').dialog(
{
    autoOpen: false
    , modal: true
    , show: 'fade'
    , hide: 'fade'
});

function openInDialog(element, event, target)
{
    event.preventDefault();
    var $loading = $('<img src="../../Others/images/ajaxLoading.gif" alt="loading" class="ui-loading-icon">');
    var $url = target;
    var $title = "Title"; 
    $dialog.empty();
    /* this is incorrect $dialog.dialog({ "option", "title",$title})*/
    $dialog.dialog("option", "title",$title)
    .append($loading)
    .load($url,function(){
        $dialog.dialog('open');
    });
};
0

精彩评论

暂无评论...
验证码 换一张
取 消