简体   繁体   English

当我的表单中有 onsubmit 事件时,为什么我的 HTML 表单不能正确地将用户输入发送到 Google 表格?

[英]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 表格中,如下所示:

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.

相关问题 当我从onsubmit返回false时,为什么我的HTML表单仍然会提交? - Why does my HTML form still submit when I return false from onsubmit? 为什么我的表单无法验证提交? - Why is my form not validating onsubmit? 为什么我的表单验证不起作用(onsubmit不触发)? - Why is my form validation not working (onsubmit not triggering)? 为什么在调用OnSubmit时必​​须返回函数的返回值? - Why I have to return the return of my function when calling OnSubmit? 在 Google 表格中,我的侧边栏中有一个未提交数据的表单。 我的代码有什么问题? - In Google sheets I have a form in my sidebar that is not submitting the data. What is wrong with my code? 为什么我的表单将制表符添加到发送的数据中? - Why does my form add tabs to the data I send it? 如何将多个html表单发送到同一文件/数据库中的不同谷歌表? 我有一个谷歌表,总共有 3 张 - How to send multiple html form to different google sheet in the same file/database? I have a google sheet which have 3 sheets in total 反应:我表单的 onSubmit 处理程序中的 Function 无法正常工作 - React: Function inside my form's onSubmit handler not working properly 我不知道为什么当单击此按钮时,它不会将表单发送到Google工作表 - I can't figure out why when this button is clicked it wont send the form to google sheets 为什么在我填写最后一个输入时,即使所有其他输入都为空,我的表单也仍然有效? - Why does my form validate when I fill in the very last input, even if all the others are empty?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM