Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将HTML表单单选和复选框按钮选择写入Google表单_Javascript_Html_Forms_Google Apps Script_Google Sheets - Fatal编程技术网

Javascript 将HTML表单单选和复选框按钮选择写入Google表单

Javascript 将HTML表单单选和复选框按钮选择写入Google表单,javascript,html,forms,google-apps-script,google-sheets,Javascript,Html,Forms,Google Apps Script,Google Sheets,我创建了一个HTML表单,它通过GoogleApps脚本成功地将数据提交到GoogleSheets 问题是我看不到选中了哪个单选按钮或复选框按钮。我得到的唯一结果是在哪个字段设置中检查按钮。只有标签上的被写入,如下所示 如何将每个按钮选择写入工作表中的相应列中,例如,我可以在foobar列中查看foo,bar,baz,以及xyzy列中查看qux HTML表单的代码: const脚本URL= "https://script.google.com/macros/s/AKfycbzz-KveHd

我创建了一个HTML表单,它通过GoogleApps脚本成功地将数据提交到GoogleSheets

问题是我看不到选中了哪个单选按钮或复选框按钮。我得到的唯一结果是在哪个字段设置中检查按钮。只有
标签上的
被写入,如下所示

如何将每个按钮选择写入工作表中的相应列中,例如,我可以在
foobar
列中查看
foo
bar
baz
,以及
xyzy
列中查看
qux


HTML表单的代码:

const脚本URL=
"https://script.google.com/macros/s/AKfycbzz-KveHder1A3CX8GcqZI6GR2MQj66PDRWNKoatIET_LXNqQs/exec"
const form=document.forms[0]
表单.addEventListener(“提交”,e=>{
e、 预防默认值()
获取(脚本URL{
方法:“张贴”,
正文:新表单数据(表单)
})
.then(response=>console.log(“Success!”,response))
.catch(error=>console.error(“error!”,error.message))
})

选择Foobar
福
酒吧
巴兹
选择xyzy
库克斯
库兹
输入个人详细信息


  • 关于HTML上的单选按钮和复选框,您希望标识选中的内容
如果我的理解是正确的,这次修改怎么样?请把这看作是几个答案中的一个

修改点:
  • 对于单选按钮和复选框的标记,添加了
    value
    属性。该值与文本相同
  • doPost(e)
    的事件对象的
    参数的属性中检索值
修改脚本: 请按如下方式修改您的脚本

HTML: 发件人: 致: 结果:
  • 当上述修改点反映到脚本中时,“foobar”和“xyzy”列的值将成为单选按钮和复选框的值。
    • 例如,选中“Foo”和“Bar”复选框,选中“quoz”单选按钮,“foobar”和“xyzy”列的值分别为“Foo,Baz”和“quoz”

如果我误解了你的问题,而这不是你想要的方向,我道歉。

非常感谢!你认为这是最优雅的方式吗?如果没有,你能把我送到更好的方向吗?@Tzar谢谢你的回复。我很高兴你的问题解决了。我不确定这是否是“最优雅的方式”。但我认为这种方式更适合你的情况。我想对我这样想的原因作如下评论。@Tzar尽管我认为也有修改每个标记的
name
属性的方法,但在这种情况下,需要使用
e.parameter[header]
。如果所有单选按钮和复选框都是独立的,则这种方式是合适的。但是,在您的例子中,您希望将2个单选按钮标识为一个组,并希望将3个复选框标识为一个组。在这种情况下,我认为使用
value
e.parameters[header]
属性更合适。如果我的评论没有用,我道歉。再次感谢您提供了非常明确的答案。你真是太好了。阿里加托!
<fieldset>
  <legend>Select Foobar</legend>
  <label><input type="checkbox" name="foobar" id="foo">Foo</label>
  <label><input type="checkbox" name="foobar" id="bar">Bar</label>
  <label><input type="checkbox" name="foobar" id="baz">Baz</label>
</fieldset>
<fieldset>
  <legend>Choose Xyzzy</legend>
  <label><input type="radio" name="xyzzy" id="quux">Quux</label>
  <label><input type="radio" name="xyzzy" id="quuz">Quuz</label>
</fieldset>
<fieldset>
  <legend>Select Foobar</legend>
  <label><input type="checkbox" name="foobar" id="foo" value="Foo">Foo</label>
  <label><input type="checkbox" name="foobar" id="bar" value="Bar">Bar</label>
  <label><input type="checkbox" name="foobar" id="baz" value="Baz">Baz</label>
</fieldset>
<fieldset>
  <legend>Choose Xyzzy</legend>
  <label><input type="radio" name="xyzzy" id="quux" value="Quux">Quux</label>
  <label><input type="radio" name="xyzzy" id="quuz" value="Quuz">Quuz</label>
</fieldset>
var newRow = headers.map(function(header) {
  return header === "timestamp" ? new Date() : e.parameter[header]
})
var newRow = headers.map(function(header) {
  return header === "timestamp" ? new Date() : e.parameters[header].join(",");
})