简体   繁体   English

在 Google Apps 脚本 web 应用程序中动态添加 Materialize select 输入时出错

[英]Error dynamically adding Materialize select input in Google Apps Script web app

I try to dynamically add extra Materialize "select"s to a custom form created with a Google Apps Script web app.我尝试将额外的 Materialize“选择”动态添加到使用 Google Apps 脚本 web 应用程序创建的自定义表单中。 Although the initial one displays OK, on adding extra (via the "Add Row" button) the new select has two sets of options.虽然最初的显示 OK,但在添加额外的(通过“添加行”按钮)时,新的 select 有两组选项。

Can anyone see what I am doing wrong?谁能看到我做错了什么?

Web 应用选择错误

Demo app 演示应用

Code.gs代码.gs

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

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

Page.html页.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 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>

About the issue of Although the initial one displays OK, on adding extra (via the "Add Row" button) the new select has two sets of options.关于问题Although the initial one displays OK, on adding extra (via the "Add Row" button) the new select has two sets of options. . . I think that the reason might be that the same id is used in invoice-form appended by appendChild(documentFragment) .我认为原因可能是在appendChild(documentFragment)附加的invoice-form中使用了相同的id So how about the following modification?那么下面的修改呢?

From:从:

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

To:至:

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)

Note:笔记:

  • But I'm not sure whether this is the direction you expect.但我不确定这是否是您期望的方向。 So if this was not the direction you expect, please tell me.因此,如果这不是您期望的方向,请告诉我。 I would like to modify it.我想修改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Google Apps脚本网络应用中动态插入脚本? - Dynamically inserting script in Google Apps Script web app? 在Google App脚本中为文本框输入动态添加蒙版? - Dynamically adding a mask to textbox input in google app script? Google Apps Script Web 应用程序中的同时用户 - Simultaneous Users in a Google Apps Script Web App web 应用程序的 Google 应用程序脚本无法正常工作 - Google apps script for web app not working 在Google Apps脚本Web应用中使用Google Visualization API显示GSheet范围时出错 - Error showing GSheet range using Google Visualization API in Google Apps Script Web App Google Apps脚本可作为Web应用程序使用,但不能作为电子表格脚本使用 - Google Apps Script Works as Web App but Not Spreadsheet Script 添加用户输入而不四舍五入(Google Apps脚本) - Adding User input without rounding (Google Apps Script) Materialise CSS 的自动完成功能不起作用(Javascript + Google Apps 脚本) - Autocomplete from Materialize CSS not working (Javascript + Google Apps Script) 物化选择-动态添加选项时不起作用 - Materialize Select - doesn't work when dynamically adding option 在Google Apps脚本网络应用中显示文件和文件夹 - Display files and folders in Google Apps Script web app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM