3

我正在尝试基于两个不同的下拉列表编写 if/and 语句。是否可以用下面的逻辑写一些东西?“if &&”语句不能正常工作,但可以与单个“if”语句一起正常工作。我知道这是一些代码,但这是我可以准确显示正在发生的事情的唯一方法。

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

  var containerSizeList = app.createListBox().setId('containerSizeList').setName('containerSizeList');
  containerSizeList.addItem("20ft Long");
  containerSizeList.addItem("40ft Long");

  var loadingStyleList = app.createListBox().setId('loadingStyleList').setName('loadingStyleList');
  loadingStyleList.addItem("On Pallets");
  loadingStyleList.addItem("Floor Loaded");

    var tenPalletPanel = app.createHorizontalPanel().setId('tenPalletPanel').setVisible(true);
    var tenPalletLabel = app.createLabel('10 Pallets Available').setId('tenPalletLabel');
    var twentyPalletPanel = app.createHorizontalPanel().setId('twentyPalletPanel').setVisible(false);
    var twentyPalletLabel = app.createLabel('20 Pallets Available').setId('twentyPalletLabel');
    var loadingNotePanel = app.createHorizontalPanel().setId('loadingNotePanel').setVisible(false);
    var loadingNoteLabel = app.createLabel('Note: Only certain products may be floor loaded')
    .setId('loadingNoteLabel');

    var containerGrid = app.createGrid(1, 2);
    containerGrid.setWidget(0, 0, containerSizeList);
    containerGrid.setWidget(0, 1, loadingStyleList);

    var handlerJ = app.createServerClickHandler('palletChangeMe'); 
    containerSizeList.addChangeHandler(handlerJ);
    loadingStyleList.addChangeHandler(handlerJ);
    handlerJ.addCallbackElement(containerGrid);       

  app.add(containerGrid);
  app.add(tenPalletPanel);
  tenPalletPanel.add(tenPalletLabel);
  app.add(twentyPalletPanel);
  twentyPalletPanel.add(twentyPalletLabel);
  app.add(loadingNotePanel);
  loadingNotePanel.add(loadingNoteLabel);

  return app;
}

function palletChangeMe(e){
  var app = UiApp.getActiveApplication();

  if (e.parameter.containerSizeList == "40ft Long" && e.parameter.loadingStyleList == "On Pallets"){
    app.getElementById('tenPalletPanel').setVisible(false);
    app.getElementById('twentyPalletPanel').setVisible(true);
    app.getElementById('loadingNotePanel').setVisible(false);
}

  return app;
}
4

1 回答 1

10

代替:

if (e.parameter.containerSizeList == "20ft Long"){
  and if (e.parameter.loadingStyleList == "On Pallets"){

应该:

if (e.parameter.containerSizeList == "20ft Long" && e.parameter.loadingStyleList == "On Pallets"){

注意:&&表示逻辑与。在 MDN阅读有关逻辑运算符的更多信息。

并且您需要删除}每个 if 语句中的最后一个,因此最终代码将如下所示:

if (e.parameter.containerSizeList == "20ft Long" && e.parameter.loadingStyleList == "On Pallets"){
    app.getElementById('tenPalletPanel').setVisible(true);
    app.getElementById('twentyPalletPanel').setVisible(false);
}

if (e.parameter.containerSizeList == "40ft Long" && e.parameter.loadingStyleList == "On Pallets"){
    app.getElementById('tenPalletPanel').setVisible(false);
    app.getElementById('twentyPalletPanel').setVisible(true);
}
于 2012-12-15T16:38:32.163 回答