我的网站上有一个计数器和一个带有“发送”按钮的文本字段。我通过 id 获取值并将它们写入变量。然后到一个对象。但我无法正确编写将此对象发送到应用程序脚本。 请帮助我了解哪里有错误或在哪里可以找到代码示例。
<!DOCTYPE html>
<html>
<head>
<title>Counter and text field</title>
</head>
<body>
<h2> Major: <span class="counter" id="counter18">0</span></h2>
<button class="button-plus_minus" onclick="incrementCounter(18)">+</button>
<button class="button-plus_minus" onclick="decrementCounter(18)">-</button>
<textarea cols="20" rows="1" class="text-field_input" type="information" name="information" id="information" autocomplete="off"
placeholder="Write information"></textarea>
<button onclick="Send()">Send</button>
</body>
<script>
function incrementCounter(counterNumber) {
let counter = parseInt($('#counter' + counterNumber).text());
counter++;
$('#counter' + counterNumber).text(counter);
}
function decrementCounter(counterNumber) {
let counter = parseInt($('#counter' + counterNumber).text());
counter--;
$('#counter' + counterNumber).text(counter);
}
function Send(){
var information = document.getElementById('information');
var counter18value = document.getElementById('counter18');
var data = {
information:information,
counter18value:counter18value
}
fetch('https://script.google.com/macros/s/XXX/exec'), {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
}
}
</script>
</html>
我希望变量能够进入应用程序脚本,从那里将它们插入到谷歌表中。但我无法做到这一点,因为它们无法到达应用程序脚本。
Send
函数中,您尝试使用
information
检索 counter18value
和 getElementById
的值,但缺少 .value
来实际获取输入元素的值。var information = document.getElementById('information').value;
var counter18value = document.getElementById('counter18').textContent;
您的
fetch
调用中存在语法错误。 URL 和选项对象应位于同一组括号内。
fetch('https://script.google.com/macros/s/XXX/exec', {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
});