我正在尝试使用 ClientHandler.validateMatches() 方法来隐藏/显示和启用/禁用 ui 的某些元素。我正在添加两个 ClientHandler,但实际上只有一个似乎按预期执行。代码如下:
function doGet(e) {
var app = UiApp.createApplication();
var grid = createAdultInfoGrid(135, 8);
app.add(grid);
return app;
}
/* Element id for adult-specific information grid element */
var GRID_ADULTS_ID = 'G_AdultInfo';
/* Element id for driver status field */
var PARTICIPANT_DRIVER_ID = 'P_IsDriver';
/* Element id for seatbelt count field */
var PARTICIPANT_SEATBELT_COUNT_ID = 'P_BeltCount';
/* Element id for seatbelt count status message field */
var PARTICIPANT_SEATBELT_MSG_ID = 'P_BeltCountMsg';
/**
* Creates a new grid for display of adult-specific information using the GRID_ADULTS_ID constant as its id
* @param pLblWidth - the width to be used for labels
* @param pCellPadding - the value to be used for cell padding in the resulting grid
*/
function createAdultInfoGrid(pLblWidth, pCellPadding) {
Logger.log("Creating adult participant information grid . . .");
var app = UiApp.getActiveApplication();
var grid = app.createGrid(2, 3).setId(GRID_ADULTS_ID).setCellPadding(pCellPadding);
Logger.log("Creating listbox - driver . . .");
var lstBoxDriver = app.createListBox().setId(PARTICIPANT_DRIVER_ID).setName('DriverStatus');
lstBoxDriver.setVisibleItemCount(1);
lstBoxDriver.addItem('Yes');
lstBoxDriver.addItem('No');
lstBoxDriver.setItemSelected(0, true);
Logger.log("Creating textbox - seatbelts . . .");
var txtBoxSeatBelts = app.createTextBox().setId(PARTICIPANT_SEATBELT_COUNT_ID).setValue('2');
var labelBeltStatus = app.createLabel('Please enter a number between 1 and 15.').setId(PARTICIPANT_SEATBELT_MSG_ID).setWidth(pLblWidth * 2).setVisible(false);
var driverYesHandler = app.createClientHandler()
.validateMatches(lstBoxDriver, '([Yy][Ee][Ss]){1}')
.forTargets(txtBoxSeatBelts)
.setText('')
.setEnabled(true)
.setVisible(true)
.setStyleAttribute("color", "black")
.setStyleAttribute("background", "white")
.forTargets(labelBeltStatus)
.setVisible(false);
var driverNoHandler = app.createClientHandler()
.validateMatches(lstBoxDriver, '([Nn][Oo]){1}')
.forTargets(txtBoxSeatBelts)
.setText('0')
.setEnabled(false)
.setVisible(true)
.setStyleAttribute("color", "black")
.setStyleAttribute("background", "white")
.forTargets(labelBeltStatus)
.setVisible(false);
lstBoxDriver.addChangeHandler(driverYesHandler);
lstBoxDriver.addChangeHandler(driverNoHandler);
Logger.log("Adding listbox - driver . . .");
grid.setWidget(0, 0, app.createLabel('I am willing to drive:').setWidth(pLblWidth));
grid.setWidget(0, 1, lstBoxDriver);
Logger.log("Adding textbox - seatbelts . . .");
grid.setWidget(1, 0, app.createLabel('Seatbelts:').setWidth(pLblWidth));
grid.setWidget(1, 1, txtBoxSeatBelts);
grid.setWidget(1, 2, labelBeltStatus);
app.close();
return grid;
}
在浏览器中访问时,我可以将列表框从“是”更改为“否”,并且 ClientHandler 工作(文本设置为 0,字段被禁用等)。但是,当列表框从“否”更改为“是”时,不会执行预期的操作 - 表单保持相同的状态。我尝试使用 'Yes' 和 'No' 作为指定了 'i' 标志的正则表达式模式 - 也尝试使用 validateNotMatch() 方法,但无济于事。在从默认值进行第一次更改后,该事件似乎没有被捕获。任何建议,将不胜感激。
在此先感谢,
dgg