0

如果这是基本的,我很抱歉。我是自学成才,并且对可能是 Javascript 编程的简单功能感到困惑。谁能告诉我我应该寻找什么来找到正确的答案,或者帮助我解决这个问题?

我在 UI 窗口中有一个函数,可以用面板快速填充它。但我似乎无法让输入到每个面板中的值从 UI 中移出并进入主“DoSomething”功能。
变量 (dlg.tryThis) 不会更新?为什么?我需要做什么?

// making a simple UI
function UI_Builder(doSomething)
{
    var dlg = new Window( "dialog", "options" );
    dlg.mainGroup = dlg.add( "group", undefined,"main" );

//Created a simple function so I can re-create panels and values very easy
    function makePanel (panelName, val)  {
        var newPanel = eval("dlg.mainGroup." + panelName);
        newPanel = dlg.mainGroup.add( 'panel', undefined, panelName );
        newPanel.Start = newPanel.add("edittext", [0,0,30,20], val);
        dlg.Start = newPanel.Start.text
        newPanel.Start.onChanging = function()                
                { 
                 dlg.Start = newPanel.Start.text
                  alert("Changed " + panelName + " to:  " + dlg.Start) // THIS alerts correctly.  It know what value I changed and where I changed it. 
                 return dlg.Start;
                 } 
            return dlg.Start;
        } 

    // calling the function to create panels.
    dlg.tryThis = makePanel("tryThis", 4); 
    dlg.anotherOne = makePanel("anotherOne",  3); 
    dlg.again = makePanel("again", 2); 

    dlg.mainGroup.btnCancel = dlg.mainGroup.add( "button",[0,0,130,70], "doSomething" );
    dlg.mainGroup.btnCancel.onClick = function()
    {  
        doSomething();  
     }
 return dlg
}

// all the UI builder is doing is showing the UI
var dlg = UI_Builder(doSomething);
dlg.show();

function doSomething()
{
dlg.close();

alert(dlg.tryThis)  // this is not the NEW value?   How do I get it to be the NEW value updated by the ".onChanging" function? 
}
4

2 回答 2

0

尝试传递dlg给事件处理程序而不是全局声明它:

dlg.mainGroup.btnCancel.onClick = function() {
    doSomething(dlg);  
}

function doSomething(dlg) {
    dlg.close();
    alert(dlg.tryThis);
}

如果您采用这种方法,则不应var dlb全局声明,以避免混淆。(阴影变量通常是一种不好的做法)

于 2012-07-23T18:56:23.527 回答
0

你在没有任何上下文的情况下调用 doSomething 。

使用 func.apply:

dlg.mainGroup.btnCancel.onClick = function()
{  
    doSomething.apply(this, []);
}

并将对象作为“this”处理:

function doSomething()
{
    this.close();
    alert(this.tryThis)  // this is not the NEW value?   How do I get it to be the NEW value updated by the ".onChanging" function? 
}
于 2012-07-23T18:58:08.580 回答