JavaScript 对话框返回

标签 javascript function return-value extendscript adobe-scriptui

我有一个绘制对话框的 JavaScript 函数。我想让它返回用户指定的值。问题是,当用户单击两个按钮时对话框关闭,这两个按钮分配有 onClick 事件。我知道获取这些事件的唯一方法是为它们分配函数,这意味着返回会导致分配的函数返回,而不是我的 inputDialog 函数。我确定我只是在以一种愚蠢的方式这样做。

如果您想知道,这个脚本使用 Adob​​e 的 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();

}

最佳答案

我想出了一个解决方案。这真的很丑陋,但它现在会让我度过难关...有人有更好的解决方案吗?

    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;

关于JavaScript 对话框返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2359181/

相关文章:

javascript - 动态设置具有 3 种颜色的 div 样式

javascript - GWT 可视化核心图表包和 SmartGWT 的问题

Python 方法调用

c++ - 为什么有些类方法返回 "*this"(自身的对象引用)?

ruby - 计数的目的是什么? 7号线

debugging - 如何检查GDB中函数的返回值?

javascript - 从 AngularJS 中的指令添加指令

javascript - Jquery按钮切换显示隐藏div

javascript - 在 Javascript 中执行作为参数传递的函数?

c++ - 如何在 x86 程序集中调用函数并向其传递参数