[英]Why does my HTML form not send user input to Google Sheets properly when I have an onsubmit event in my form?
I am currently making an expense tracker.我目前正在制作费用跟踪器。 The HTML form with user input sends data to a Google Sheet and simultaneously executes the following
addExpense()
function;带有用户输入的 HTML 表单将数据发送到 Google 表格并同时执行以下
addExpense()
function;
function addExpense()
{
clothingID={'id':Date.now(),'clothingType':clothingType.value, 'brand': brand.value, 'amnt':amnt.value, };
clothingArray.push(clothingID);
count1++;
showTotalBalance();
showDifference();
expenseHistory();
clothingType.value='';
amnt.value='';
brand.value = '';
setItemInStorage();
}
The HTML form looks like this without the onsubmit
event:没有
onsubmit
事件的 HTML 表单看起来像这样:
<form method = "POST" action = 'https://script.google.com/macros/s/AKfycbwo9AKU8OB5YXs_l0w3wqKoZDYp6F5C3EuarHeQVILt4CK9zaIhmGQETz7StlOc2P/exec' >
<div class="row">
<div class="six columns">
<label>Clothing type</label>
<input name = "Type" id = 'type' style = "width: 50%;" type="text" placeholder="Enter the clothing type" id="exampleEmailInput">
</div>
</div>
<div>
<label>Amount</label>
<input name = "Amount" id="amnt" style = "width: 24%;" type="number" placeholder="Enter number" id="exampleEmailInput">
</div>
<div>
<label style = 'padding-bottom: 1pt;' for="exampleEmailInput">Clothing brand</label>
<input name = "Store" id="brand" style = "width: 24%;" type="text" placeholder="Enter the clothing brand" id="exampleEmailInput">
</div>
<button type = 'submit' class="button-primary">Add expense</button>
</form>
When I submit this form, the data is placed into a Google Sheet without problems, as such:当我提交此表单时,数据会毫无问题地放入 Google 表格中,如下所示:
However, I would like the form to both execute the addExpense()
function, and to also send the data to Google Sheets.但是,我希望表单既能执行
addExpense()
function,又能将数据发送到 Google 表格。 Here is what happens when the addExpense()
function is executed upon form submission:以下是在提交表单时执行
addExpense()
function 时发生的情况:
This is only achievable when the form element looks like this:这只有在表单元素如下所示时才能实现:
<form onsubmit='addExpense()'>
When I place both the action
attribute and the onsubmit
event to my form as such:当我将
action
属性和onsubmit
事件都放置到我的表单中时:
<form method="POST" action ="https://script.google.com/macros/s/AKfycbwo9A2AKU8OB5YXs_l0w3wqKoZDYp6F5C3EuarHeQVILt4CK9zaIhmGQETz7StlOc2P/exec" onsubmit='addExpense()'>
I only get the date (generated by the Date()
object) returned in my Google Sheet, but the addExpense()
function works fine:我只得到我的 Google 表格中返回的日期(由
Date()
对象生成),但addExpense()
function 工作正常:
Any ideas on why this is occuring, and how I can fix this?关于为什么会发生这种情况以及如何解决这个问题的任何想法?
These lines:这些行:
clothingType.value='';
amnt.value='';
brand.value = '';
clothingType, amnt, and brand are live references to the input elements in question. clothingType、amnt 和 brand 是对相关输入元素的实时引用。 Setting the value to empty string will have the effect that when you submit the form, those entries will be blank (because you set them to blank.).
将值设置为空字符串会产生这样的效果,即当您提交表单时,这些条目将为空白(因为您将它们设置为空白。)。
I think the reason you are doing this is to clear out the form once it's submitted.我认为您这样做的原因是在提交表格后清除表格。 However I don't think you need to, usually once you submit and reload a form it will be cleared out automatically.
但是我认为您不需要,通常一旦您提交并重新加载表单,它就会自动清除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.