简体   繁体   English

Google Apps 脚本 - 表单输入未正确提交

[英]Google Apps Script - Form inputs not submitting properly

<form id="colours" onsubmit="doSomething()">
  <label for="startcolour" style="font-family:arial">Start:</label>
  <input type="color" name="startcolour" id="startcolour" value="#123456"> &nbsp;
  <label for="endcolour" style="font-family:arial">End:</label>
  <input type="color" name="endcolour" id="endcolour" value="#abcdef"><br><br>
  <br>
  <input type="submit" value="Submit">
</form>

<script>
  var startcolour = document.getElementById("startcolour").value;
  var endcolour = document.getElementById("endcolour").value;

  function doSomething() {
    google.script.run.withFailureHandler(function(error) {
      console.log("error")
      console.log(error.message)
    }).withSuccessHandler(function(result) {
      console.log("done!")
    }).test(startcolour);
  }
</script>

I have a Google Apps Script HTML sidebar.我有一个 Google Apps Script HTML 侧边栏。 This is some of the code for the HTML sidebar.这是 HTML 侧边栏的一些代码。 When I enter a colour and press 'Submit', I want it to run the test() function with the submitted value for startcolour as the input.当我输入颜色并按“提交”时,我希望它以提交的 startcolour 值作为输入运行 test() 函数。 However, it runs with '#123456', the default value, not the value the user submitted.但是,它使用默认值“#123456”运行,而不是用户提交的值。 How do I fix this?我该如何解决?

The problem is that you are defining the "startcolour" and "endcolour" variables from outside of your doSomething function, so when that function is called, it uses the value that was defined when the page was loading, instead of a value that you could define when the form is being submitted.问题是您正在从 doSomething 函数外部定义“startcolour”和“endcolour”变量,因此当调用该函数时,它使用页面加载时定义的值,而不是您可以使用的值定义何时提交表单。

The solution would be to define the variables "startcolour" and "endcolour" within your doSomething function.解决方案是在 doSomething 函数中定义变量“startcolour”和“endcolour”。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM