JavaScript 对话框返回

发布于 2024-08-23 13:51:47 字数 1621 浏览 7 评论 0原文

我有一个绘制对话框的 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();

}

I've got a JavaScript function that draws a dialog. I'd like to have it return the value the user specifies. The problem is, the dialog is closed when a user clicks on of two buttons, which have onClick events assigned to them. The only way I know to grab these events is by assigning functions to them, which means that a return causes the assigned function to return, and not my inputDialog function. I'm sure I'm just doing this in a stupid way.

In case you're wondering, this script is using Adobe's ExtendScript API for extending After Effects.

Here's the code:

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();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七度光 2024-08-30 13:51:47

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

    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;

I've come up with a solution. It's really ugly, but it'll tide me over for now... Anyone have a better solution?

    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;
笑着哭最痛 2024-08-30 13:51:47

使用特定整数关闭对话框可能会稍微好一些。然后,您可以检查 show() 的返回值并返回适当的值。请参阅 jongware 对 ExtendScript ScriptUI 窗口的关闭方法 的引用。

// Register event listeners that define the button behavior
dia.okBtn.onClick = function() {
    if(dia.input.text != "") { //check that the text input wasn't empty
        dia.close(1); //close the window
    } 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(2);
};


var buttonCloseValue = dia.show(); //blocks until user interaction
if(dia.show() == 1) {
    if(debug) alert(dia.input.text);
    return dia.input.text;
}
return false;

It might be slightly better to close the dialog with a specific integer. You can then check the return value from show() and return the appropriate value. See jongware's reference to ExtendScript ScriptUI Window's close method.

// Register event listeners that define the button behavior
dia.okBtn.onClick = function() {
    if(dia.input.text != "") { //check that the text input wasn't empty
        dia.close(1); //close the window
    } 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(2);
};


var buttonCloseValue = dia.show(); //blocks until user interaction
if(dia.show() == 1) {
    if(debug) alert(dia.input.text);
    return dia.input.text;
}
return false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文