2011-05-25 59 views
1

我想用jQuery對話框在模態對話框中打開答案窗體。第一次加載頁面後,沒關係,但之後每次點擊都會打開2^n-1次!!! (n是點擊數)爲什麼我的模態jQuery對話框打開多次?

是這樣的:

單擊 - >打開對話框(1次) - >關閉對話框

單擊 - >打開對話框(2次) - >關閉對話框

點進>打開的對話框(4次) - >關閉對話框

點進>打開的對話框(8次) - >關閉對話框

這是代碼:

$(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'); 
}; 
+0

'var $ dialog = $('

');'不看起來腥嗎? ;) – 2011-05-25 09:20:14

+0

@Alireza:同事,不是大學(起訴我拼寫錯誤)。 http://m-w.info/dictionary/colleague我只是基於在這裏參與了很多,給你一個意見,說這個級別的俚語是不恰當和不尊重的。 – 2011-05-25 12:17:43

+0

@ T.J。 Crowder - 謝謝,我不會再用俚語了。 – Alireza 2011-05-25 13:01:13

回答

4

初始化對話的功能之外。在加載成功之前,您也不應嘗試打開對話框。

$(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

你的權利,這是問題所在。謝啦。 – Alireza 2011-05-25 10:03:36

相關問題