2012-01-03 55 views
1

我想在選擇自動完成項目後讓Jquery UI模式對話框接管。除了模式對話框啓動後,我的所有內容都會彈出,但有代碼正在運行。最後,我想讓這個對話框彈出並根據按鈕作出反應。Jquery - 選擇自動完成期間使用模式對話框

從選擇自動完成:

 select: function(event, ui) { 
     if(ui.item.squad != '0'){ 
      console.info('popup'); 
      var choice=null; 

      $("#dialog-confirm").dialog({ 
       resizable: false, 
       height:140, 
       modal: true, 
       buttons: { 
        Cancel: function() { 
         choice = false; 
         $(this).dialog("close"); 
        }, 
        "Move Shooter": function() { 
         choice = true; 
         $(this).dialog("close"); 

        } 
       } 
      }); 
      if(!choice){ 
       console.info($(this)); 
       $(this).text(""); 
       $(this).val(""); 
       $(this).attr("name", ""); 
       $(this).attr("value", ""); 
       console.info("false"); 
       return; 
      } 
     } 

大部分代碼已經從jQuery UI的拍攝here.

當我運行此代碼,我認爲它會停止運行代碼,直到一個按鈕是按下但您可以看到here將打印出錯誤的控制檯打印出來。

回答

0

你的代碼不會工作,因爲它會執行整個塊並返回。你想要的是定義一個函數,一旦用戶做出選擇就執行。本質上,您可以通過將您在對話框代碼下的if聲明移至其自己的函數中,然後將此函數作爲按鈕處理程序代碼的一部分調用。

例子:

定義某處回調:

function handleDialogResponse(choice) { 
    if(!choice){ 
     console.info($(this)); 
     $(this).text(""); 
     $(this).val(""); 
     $(this).attr("name", ""); 
     $(this).attr("value", ""); 
     console.info("false"); 
    } 
} 

您的對話框按鈕的代碼然後切換到這樣的事情:

buttons: { 
    Cancel: function() { 
     $(this).dialog("close"); 

     handleDialogResponse(false); 
    }, 
    "Move Shooter": function() { 
     $(this).dialog("close"); 

     handleDialogResponse(true); 
    } 
} 
+0

而你是我的英雄。謝謝您的幫助 – 2012-01-03 22:46:41