使用单个按钮切换小部件可见性

时间:2012-12-20 12:54:51

标签: google-apps-script

以下代码应该在单击按钮时将文本框中找到的true / false的反转写回文本框 - 但它无法正常工作。它将以一种方式正常工作,而不是另一种方式(有效的方法是最后定义的ClickHandler)。我也尝试过使用validateNotMatches,但没有快乐。

如果我更改代码以便更新标签的文本而不是文本框,则可以正常工作。

我知道建议的解决方法,例如使用两个按钮,但我只是想知道我做错了什么,或者这看起来像是GAS中的错误。感谢。

function doGet(e)
{
  var app = UiApp.createApplication();

  var tb = app.createTextBox().setText('true');
  var button = app.createButton('button');
  var label = app.createLabel().setText('-');

  button.addClickHandler(app.createClientHandler()
                  .validateMatches(tb, 'true')
                  //.forTargets(label).setText('false')
                  .forTargets(tb).setText('false')
                 );
  button.addClickHandler(app.createClientHandler()
                  .validateMatches(tb, 'false')
                  //.forTargets(label).setText('true')
                  .forTargets(tb).setText('true')
                 );

  return app.add(app.createHorizontalPanel().add(tb).add(button).add(label));
}

1 个答案:

答案 0 :(得分:2)

这不是一个错误......两个事件都会发生。验证发生在窗口小部件的当前状态,而不是事件被触发时的状态,因此在您将其翻转为“false”后,第二个处理程序将验证并将其翻转回“true”。

相关问题