首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用HTML和Google脚本发送表单

如何用HTML和Google脚本发送表单
EN

Stack Overflow用户
提问于 2016-05-07 00:05:00
回答 1查看 1K关注 0票数 1

如何使用HTML和GS在此代码中发送表单?当我点击'Submit‘时它不会发送。我认为'send()‘函数有错误,或者表单有错误。请留下答案。提前感谢!

HTML:

代码语言:javascript
复制
<!DOCTYPE html>
<!-- Quiz By Yona Klatchko -->
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <p class="BoldVerdana">
      Welcome to Yona's Geography Quiz.
    </p>
    <p class="BoldVerdana">
      Begin whenever you are ready. 
    </p>
    <br/>
    <script>
     function send() {
         var emailAddress = document.getElementById('emailAddress').value;
         var result = document.getElementById('Questions').value;
         google.script.run.send(emailAddress, result);
     }
    </script>
    <style>
      p.BoldVerdana {
          font-weight: bold;
          font-family: "Verdana";
      }
    </style>
    <br/>
    <font face="Verdana">
      Your teacher's email address
    </font>
    <br/>
    <br/>
    <input type="name" id="emailAddress"/>
    <br/>
    <br/>
    <div id="answers">
      <form name="Questions" id="Questions" method="post" action="EmailAddress; subject=Geography Quiz Answers enctype='text/plain'">
        <p class="BoldVerdana">
          Input questions
        </p>
        <font face="Verdana">
          1. Which state has a climate suitable for growing citrus fruits — California or Maine?
        </font>
        <br/>
        <br/>
        <input type="text" id="Ia1">
        <br/>
        <br/>
        <br/>
        <font face="Verdana">
          2. The North Atlantic current brings warm waters from the tropics to the west coast of which continent?
        </font>
        <br/>
        <br/>
        <input type="text" id="Ia2">
        <br/>
        <br/>
        <br/>
        <font face="Verdana">
          3. Which Canadian province produces more than half of the country's manufactured goods?
        </font>
        <br/>
        <br/>
        <input type="text" id="Ia3">
        <br/>
        <br/>
        <br/>
        <font face="Verdana">
          4. To visit the ruins of Persepolis, an ancient ceremonial capital of Persia, you would have to travel to what present-day country?
        </font>
        <br/>
        <br/>
        <input type="text" id="Ia4">
        <br/>
        <br/>
        <br/>
        <font face="Verdana">
          5. What is the term for a part of an ocean or sea that cuts far into the bordering landmass and may contain one or more bays?
        </font>
        <br/>
        <br/>
        <input type="text" id="Ia5">
        <br/>
        <br/>
        <br/>
        <p class="BoldVerdana">
          Multiple choice
        </p>
        <font face="Verdana" id="Ma1">
          6. Which one of the following zones of the atmosphere is rich in Ozone gas?
          <br/>
          <input type="radio" name="6. Which one of the following zones of the atmosphere is rich in Ozone gas?" value="Mesosphere">
          Mesosphere
          <br/>
          <input type="radio" name="6. Which one of the following zones of the atmosphere is rich in Ozone gas?" value="Troposphere">
          Troposphere
          <br/>
          <input type="radio" name="6. Which one of the following zones of the atmosphere is rich in Ozone gas?" value="Ionosphere">
          Ionosphere
          <br/>
          <input type="radio" name="6. Which one of the following zones of the atmosphere is rich in Ozone gas?" value="Stratosphere">
          Heat is generated in it
		  <br/>
		  <br/>
		</font>
        <font face="Verdana" id="Ma2">
          7. Troposphere is the hottest part of the atmosphere because
          <br/>
          <input type="radio" name="7. Troposphere is the hottest part of the atmosphere because" value="It is closest to the sun">
          It is closest to the sun
          <br/>
          <input type="radio" name="7. Troposphere is the hottest part of the atmosphere because" value="It is heated by the Earth's surface">
          It is heated by the Earth's surface
          <br/>
          <input type="radio" name="7. Troposphere is the hottest part of the atmosphere because" value="There are charged particles in it">
          There are charged particles in it
          <br/>
          <input type="radio" name="7. Troposphere is the hottest part of the atmosphere because" value="Heat is generated in it">
          Heat is generated in it
		  <br/>
		  <br/>
        </font>
        <font face="Verdana" id="Ma3">
          8. Continents have drifted apart because of
          <br/>
          <input type="radio" name="8. Continents have drifted apart because of" value="Volcanic eruption">
          Volcanic eruption
          <br/>
          <input type="radio" name="8. Continents have drifted apart because of" value="Folding and Faulting of rocks">
          Folding and Faulting of rocks
          <br/>
          <input type="radio" name="8. Continents have drifted apart because of" value="Tectonic activities">
          Tectonic activities
          <br/>
          <input type="radio" name="8. Continents have drifted apart because of" value="All of the above">
          All of the above
          <br/>
        </font>
        <br/>
      </form>
    </div>
    <button onclick="send(); Submit" value="Submit">
      <font face="Verdana" size="10">
        Submit
      </font>
    </button>
  </body>
</html>

GS:

发送():

代码语言:javascript
复制
function send(emailAddress, result) {
  MailApp.sendEmail(emailAddress, result)
}

doGet:

代码语言:javascript
复制
function doGet() {
    return(HtmlService.createTemplateFromFile('Quiz.html').evaluate().setTitle('Geo Quiz').setSandboxMode(HtmlService.SandboxMode.IFRAME)); 
}
EN

回答 1

Stack Overflow用户

发布于 2016-05-07 01:13:58

两个参数不能发送到服务器。你有:

代码语言:javascript
复制
google.script.run.send(emailAddress, result);

应:

代码语言:javascript
复制
google.script.run.send([emailAddress, result]);//Put variables into an array

如果要使用method,则不应该在<form>标记中命名google.script.run.myFunctionName()google.script.run.myFunctionName()属性。

你有:

代码语言:javascript
复制
<form name="Questions" id="Questions" method="post" action="EmailAddress; subject=Geography Quiz Answers enctype='text/plain'">

应:

代码语言:javascript
复制
<form id="Questions">

另外,您不需要form元素标记中的name属性。您将通过ID获取表单对象,这是您应该做的事情。

如果要获取整个form对象,并将form对象发送到服务器,则不应使用value属性。

你有:

代码语言:javascript
复制
var result = document.getElementById('Questions').value;

应:

代码语言:javascript
复制
var result = document.getElementById('Questions');//Get the Form object

如果电子邮件输入字段在表单中,那么您可以获得整个form对象,并且不需要单独获取电子邮件地址。你有:

代码语言:javascript
复制
var emailAddress = document.getElementById('emailAddress').value;

如果您确实希望在表单对象之外的电子邮件地址输入字段,那么我将在最后解释如何做到这一点。

如果要发送表单对象,那么每个<input>都必须有一个name属性。您的表单输入元素中没有所有的名称属性。

当Apps脚本将表单对象发送到服务器时,它首先处理对象并删除除name属性作为键的所有内容,并将值作为对象的key/value元素中的值。不能通过id名称从服务器中的表单对象中获取值。

Submit元素中有<button>

代码语言:javascript
复制
<button onclick="send(); Submit" value="Submit">

它应该是:

代码语言:javascript
复制
<button onclick="send()">

您不需要value属性,因为“提交”一词位于两个button标记之间。

服务器功能不能接收两个参数。你有:

代码语言:javascript
复制
function send(emailAddress, result) {

应:

代码语言:javascript
复制
function send(theFormObject) {

服务器功能应该是:

代码语言:javascript
复制
function send(theFormObject) {
  MailApp.sendEmail(theFormObject.nameOfInput, result)
}

您在name属性中使用非常长的名称:

你有:

代码语言:javascript
复制
<input type="radio" name="6. Which one of the following zones of the atmosphere is rich in Ozone gas?" value="Mesosphere">

您想要的似乎是一个placeholder或一个输入标签。

代码语言:javascript
复制
<input type="radio" placeholder="6. Which one of the following zones of the atmosphere is rich in Ozone gas?" value="Mesosphere">

或者:

代码语言:javascript
复制
<label for="questionSix">6. Which one of the following zones of the atmosphere is rich in Ozone gas?</label>
<input type="radio" value="Mesosphere" id="questionSix">

如果您不想将电子邮件输入字段放在<form>中,那么就会出现一个问题,因为您不能向服务器发送两个单独的值。但是,您可以对form对象进行字符串化:

代码语言:javascript
复制
var result = document.getElementById('Questions');//The Form object
var myFormObject = JSON.stringify(result);
var emailAddress = document.getElementById('emailAddress').value;

然后向服务器发送一个长字符串:

代码语言:javascript
复制
var bothEmailAndOjbect = emailAddress + "," + myFormObject;
google.script.run.send(bothEmailAndOjbect);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37083060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档