2010-03-01 94 views
1

我有一個繪製對話框的JavaScript函數。我想讓它返回用戶指定的值。問題是,當用戶點擊兩個按鈕時,對話框關閉,這兩個按鈕分配有onClick事件。我知道抓住這些事件的唯一方法是給它們分配函數,這意味着返回會導致指定的函數返回,而不是我的inputDialog函數。我確信我只是以一種愚蠢的方式來做這件事。JavaScript對話框返回

如果您想知道,該腳本使用Adobe的ExtendScript API來擴展After Effects。

下面的代碼:我想出了一個解決方案

function inputDialog (queryString, title){ 
    // Create a window of type dialog. 
    var dia = new Window("dialog", title, [100,100,330,200]); // bounds = [left, top, right, bottom] 
    this.windowRef = dia; 

    // Add the components, a label, two buttons and input 
    dia.label = dia.add("statictext", [20, 10, 210, 30]); 
    dia.label.text = queryString; 
    dia.input = dia.add("edittext", [20, 30, 210, 50]); 
    dia.input.textselection = "New Selection"; 
    dia.input.active = true; 
    dia.okBtn = dia.add("button", [20,65,105,85], "OK"); 
    dia.cancelBtn = dia.add("button", [120, 65, 210, 85], "Cancel"); 


    // Register event listeners that define the button behavior 

    //user clicked OK 
    dia.okBtn.onClick = function() { 
     if(dia.input.text != "") { //check that the text input wasn't empty 
      var result = dia.input.text; 
      dia.close(); //close the window 
      if(debug) alert(result); 
      return result; 
     } else { //the text box is blank 
      alert("Please enter a value."); //don't close the window, ask the user to enter something 
     } 
    }; 

    //user clicked Cancel 
    dia.cancelBtn.onClick = function() { 
     dia.close(); 
     if(debug) alert("dialog cancelled"); 
     return false; 
    }; 

    // Display the window 
    dia.show(); 

} 

回答

0

。這真的很難看,但它現在會浪費我......任何人都有更好的解決方案?

var ret = null; 

    // Register event listeners that define the button behavior 
    dia.okBtn.onClick = function() { 
     if(dia.input.text != "") { //check that the text input wasn't empty 
      var result = dia.input.text; 
      dia.close(); //close the window 
      if(debug) alert(result); 
      ret = result; 
      return result; 
     } else { //the text box is blank 
      alert("Please enter a value."); //don't close the window, ask the user to enter something 
     } 
    }; 

    dia.cancelBtn.onClick = function() { //user cancelled action 
     dia.close(); 
     ret = false; 
     return false; 
    }; 

    // Display the window 
    dia.show(); 

    while(ret == null){}; 
    return ret;