我有一个绘制对话框的 JavaScript 函数。我想让它返回用户指定的值。问题是,当用户单击两个按钮时关闭对话框,这两个按钮onClick
分配了事件。我知道获取这些事件的唯一方法是为它们分配函数,这意味着返回会导致分配的函数返回,而不是我的 inputDialog 函数。我确定我只是以愚蠢的方式这样做。
如果您想知道,此脚本使用 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();
}