2012-01-03 56 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