2

ダイアログを描画する JavaScript 関数があります。ユーザーが指定した値を返すようにしたいと思います。onClick問題は、イベントが割り当てられた2 つのボタンをユーザーがクリックすると、ダイアログが閉じられることです。これらのイベントを取得する唯一の方法は、それらに関数を割り当てることです。つまり、return により、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();

}
4

2 に答える 2

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;
于 2010-03-01T22:20:04.213 に答える