如何读入所有复选框值?

时间:2013-10-31 12:06:02

标签: google-apps-script

我有一个表单(使用HtmlService),它有一些复选框。当它被提交时,它应该通过电子邮件发送内容。一切都适用于复选框值的豁免。仅发送第一个复选框值。我究竟做错了什么?我在下面添加了一个示例代码。任何帮助表示赞赏。

<form name=“some-form” action="<?= appUrl ?>" method="post”>
<input type=“hidden” name=“form_name” value=“send_form”>
<label>Your Name</label>
<input type=“text” name=“name”>
<label>Your Email</label>
<input type=“text” name=“email”>
<label>Favorite Colors</label>
<input type=“checkbox” name=“favorite_colors” value=“blue”>Blue<br/>
<input type=“checkbox” name=“favorite_colors” value=“red”>Red<br/>
<input type=“checkbox” name=“favorite_colors” value=“yellow”>Yellow<br/>
<input type=“checkbox” name=“favorite_colors” value=“green”>Green<br/>

<input type=“submit” value=“Send Form”>

</form>



// post function

function doPost(e)
{

    var evaluated = undefined;

    //check if right form and call send function
    if(e.parameter.form_name == “send_form”)
    {

        evaluated = sendForm(e)

    }

   return evaluated;

}


// send function

function sendForm(e)
{

    // data from form
    var name = e.parameter.name;
    var email = e.parameter.email;
    var favorite_colors = e.parameter.favorite_colors;


    // create and send message
    var to = “some-email@gmail.com”;
    var subject = “New form response”;
    var body = “A new form was completed” +
           “Name: “ + name +
           “Email: “ + email +
                   “Favorite Colors: “ + email;


    MailApp.SendEmail(to, subject, body);

}

2 个答案:

答案 0 :(得分:0)

我相信你输入的价值都是相同的:

<input type=“checkbox” name=“favorite_colors” value=“blue”>Blue<br/>
<input type=“checkbox” name=“favorite_colors” value=“blue”>Red<br/>

应该返回蓝色,蓝色。

尝试将它们设置为与您期望的结果相匹配:

<input type=“checkbox” name=“favorite_colors” value=“blue”>Blue<br/>
<input type=“checkbox” name=“favorite_colors” value=“red”>Red<br/>

答案 1 :(得分:0)

如果您使用不同的var名称:

...
<label>Favorite Colors</label><br>
<input type='checkbox' name='favorite_colors1' value='blue'>Blue<br/>
<input type='checkbox' name='favorite_colors2' value='red'>Red<br/>
<input type='checkbox' name='favorite_colors3' value='yellow'>Yellow<br/>
<input type='checkbox' name='favorite_colors4' value='green'>Green<br/>
...

并在代码中:

function sendForm(e){
  var name = e.parameter.name;
  var email = e.parameter.email;
  var favorite_colors1 = e.parameter.favorite_colors1;
  var favorite_colors2 = e.parameter.favorite_colors2;
  var favorite_colors3 = e.parameter.favorite_colors3;
  var favorite_colors4 = e.parameter.favorite_colors4;
  var dest = 'xxxx@gmail.com';
  var subject = 'New form response';
  var body = 'A new form was completed \n' +
    'Name: ' + name +
      '\nEmail: ' + email +
        '\nFavorite Colors: \n' + favorite_colors1
        +'\n'+favorite_colors2
        +'\n'+favorite_colors3
        +'\n'+favorite_colors4
    MailApp.sendEmail(dest, subject, body);
}

您将获得所有复选框结果。

test with all 4 checked :
    A new form was completed
    Name: name test
    Email: @@@@@
    Favorite Colors:
    blue
    red
    yellow
    green