在Google Apps脚本网络应用程序中动态添加具体化选择输入错误

问题描述 投票:0回答:1

我正在尝试向使用Google Apps脚本网络应用创建的自定义表单中动态添加额外的Materialize“选择”。尽管最初的显示为OK,但是在添加其他内容(通过“添加行”按钮)时,新选择具有两组选项。

谁能看到我在做什么错吗?

Web App select error

Demo app

Code.gs

function doGet(e){
  return HtmlService.createTemplateFromFile("Page").evaluate();
}

function include(filename){
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
}

Page.html

<!DOCTYPE html>
<html>

  <head>
    <base target="_top">
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  </head>

  <body onload="onLoad()">
    <div class="container">
      <div class="row">
        <button class="btn waves-effect waves-light" id ="add-row-btn" name="action">Add Row</button>
      </div>
      <form id="invoice-form" name="invoice-form"></form>      
    </div> 

    <!-- template --> 

    <div style="display: none" data-type="template" id="row-template"> 
      <div class="row">
        <div class="input-field col s2">
          <select id="HoursType" name="HoursType">
            <option value="Hours Type" disabled selected name="Hours Type">Hours Type</option>
            <option value="Indirect" name="Indirect">Indirect</option>
            <option value="Direct" name="Direct">Direct</option> 
          </select>
        </div>
      </div> <!-- row -->  
    </div> 

    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
    <?!= include("Page-js"); ?>
  </body>
</html>

Page.js.html

<script>

  var selectorCount = 0

  function onLoad() {
    addRow()
    document.getElementById("add-row-btn").addEventListener("click", addRow);
  }

  function addRow() {
    var documentFragment = document.createDocumentFragment();
    var temporaryNode = document.getElementById('row-template').cloneNode(true); //true for deep clone
    documentFragment.appendChild(temporaryNode)
    document.getElementById('invoice-form').appendChild(documentFragment)
    var elements = document.querySelectorAll('select');
    var element = elements[selectorCount++]
    selector = M.FormSelect.init(element);   
    temporaryNode.style.display = "block"
    delete documentFragment
  }

</script>
javascript html google-apps-script materialize google-apps-script-web-application
1个回答
0
投票

关于Although the initial one displays OK, on adding extra (via the "Add Row" button) the new select has two sets of options.的问题。我认为原因可能是id附加在invoice-form中使用了相同的appendChild(documentFragment)。那么下面的修改如何?

发件人:

function addRow() {
  var documentFragment = document.createDocumentFragment();
  var temporaryNode = document.getElementById('row-template').cloneNode(true); //true for deep clone
  documentFragment.appendChild(temporaryNode)

收件人:

function addRow() {
  var documentFragment = document.createDocumentFragment();
  var temporaryNode = document.getElementById('row-template').cloneNode(true); //true for deep clone

  temporaryNode.id = temporaryNode.id + selectorCount;  // Added

  documentFragment.appendChild(temporaryNode)

注意:

  • 但是我不确定这是否是您期望的方向。因此,如果这不是您期望的方向,请告诉我。我想修改它。
© www.soinside.com 2019 - 2024. All rights reserved.