点击时如何禁用按钮? [英] How to disable a button when clicked?
问题描述
我使用下面的代码创建一个表单
var app = UiApp.createApplication();
app.setTitle('My Title');
app.setHeight(150);
app.setWidth(300);
var form = app.createFormPanel();
var flow = app.createFlowPanel();
flow.add(app.createHidden(action,action));
flow.add(app.createLabel('My Label'));
//提交
var submit = app.createButton('Run',app.createServerHandler('myFunction')。addCallbackElement(form))。setId('run');
flow.add(submit);
form.add(flow);
app.add(form);
// return app;
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.show(app);
在 myFunction
我尝试使用下面的代码来引用我的按钮
function myFunction(e){
var app = UiApp。 getActiveApplication();
var submitButton = app.getElementById('run');
尝试{
submitButton.setEnabled(false);
} finally {
submitButton.setEnabled(true);
}
}
但它不起作用。我得到了下面的执行日志(不是Logger.log日志记录,但是我的意思是可以从 View> Execution transcript 显示的执行日志记录)
[14-07-03 11:04:44:091 SAST]开始执行
[14-07-03 11:04:44:124 SAST] UiApp.getActiveApplication ()[0 seconds]
[14-07-03 11:04:44:125 SAST](class).getElementById([fetch])[0 seconds]
[14-07-03 11 :04:44:125 SAST](class).setEnabled([false])[0 seconds]
[14-07-03 11:04:44:127 SAST](class).setEnabled([true] )[0秒]
[14-07-03 11:04:44:127 SAST]执行成功[总运行时间为0.002秒]
但按钮从不被禁用!我可以使用 clientHandler 可以更简单地做到这一点(并且因为客户端处理程序立即做出反应,所以也更加可靠!)
只需将其添加到您的按钮中即可:
var app = UiApp.createApplication ();
app.setTitle('My Title');
app.setHeight(150);
app.setWidth(300);
var form = app.createFormPanel();
var flow = app.createFlowPanel();
flow.add(app.createHidden(action,'action'));
flow.add(app.createLabel('My Label'));
//提交
var submit = app.createButton('Run',app.createServerHandler('myFunction')。addCallbackElement(form))。setId('run');
var cliHandler = app.createClientHandler()。forEventSource()。setEnabled(false);
submit.addClickHandler(cliHandler);
flow.add(submit);
form.add(flow);
app.add(form);
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.show(app);
请注意,表单通常使用submitButton以按预期方式工作...阅读关于它的文档。
编辑:在服务器处理函数中重新启用:
var app = UiApp.getActiveApplication();
app.getElementById('run')。setEnabled(true);
返回应用程序;
I create a form using the following code
var app = UiApp.createApplication();
app.setTitle('My Title');
app.setHeight(150);
app.setWidth(300);
var form = app.createFormPanel();
var flow = app.createFlowPanel();
flow.add(app.createHidden("action", action));
flow.add(app.createLabel('My Label'));
//Submit
var submit = app.createButton('Run', app.createServerHandler('myFunction').addCallbackElement(form)).setId('run');
flow.add(submit);
form.add(flow);
app.add(form);
//return app;
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.show(app);
In myFunction
, the button click handler, I try to refer to my button using the following code
function myFunction(e) {
var app = UiApp.getActiveApplication();
var submitButton = app.getElementById('run');
try{
submitButton.setEnabled(false);
}finally{
submitButton.setEnabled(true);
}
}
But it doesn't work. I get the following execution log (not the Logger.log logging, but I mean the execution logging that can be shown from View > Execution transcript)
[14-07-03 11:04:44:091 SAST] Starting execution
[14-07-03 11:04:44:124 SAST] UiApp.getActiveApplication() [0 seconds]
[14-07-03 11:04:44:125 SAST] (class).getElementById([fetch]) [0 seconds]
[14-07-03 11:04:44:125 SAST] (class).setEnabled([false]) [0 seconds]
[14-07-03 11:04:44:126 SAST] (class).setEnabled([true]) [0 seconds]
[14-07-03 11:04:44:127 SAST] Execution succeeded [0.002 seconds total runtime]
But the button NEVER gets disabled ! How can I disable it when its click handler is fired ?
You can use a clientHandler to do that more simply (and also more reliably since client handlers react instantaneously !)
simply add it to your button like this :
var app = UiApp.createApplication();
app.setTitle('My Title');
app.setHeight(150);
app.setWidth(300);
var form = app.createFormPanel();
var flow = app.createFlowPanel();
flow.add(app.createHidden("action", 'action'));
flow.add(app.createLabel('My Label'));
//Submit
var submit = app.createButton('Run', app.createServerHandler('myFunction').addCallbackElement(form)).setId('run');
var cliHandler = app.createClientHandler().forEventSource().setEnabled(false);
submit.addClickHandler(cliHandler);
flow.add(submit);
form.add(flow);
app.add(form);
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.show(app);
Note also that forms normally use submitButton to work as expected... read the doc about it.
EDIT : to re-enable in the server handler function :
var app = UiApp.getActiveApplication();
app.getElementById('run').setEnabled(true);
return app;
这篇关于点击时如何禁用按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!