简体   繁体   English

即使未填写必填字段,我是否可以在超时时提交表单

[英]Can I submit form on timeout even if required fields are not filled

I was wondering if it is possible to submit a form after a certain period of time (eg 2 minutes) even if not all the required fields are filled out, as I would like all the data entered by that fixed period of time to be submitted.我想知道是否可以在一段时间(例如 2 分钟)后提交表单,即使没有填写所有必填字段,因为我希望提交该固定时间段内输入的所有数据. Currently, although I'm using a timeout function that is javascript-based, it does not allow for the form to be submitted upon timeout as the required fields are not completed.目前,虽然我使用的是基于 javascript 的超时函数,但它不允许在超时时提交表单,因为未完成必填字段。 I set all the fields to required as the autofocus function does not seem to work if it is not a required field (ie does not go into the next input field automatically upon pressing enter in the current field. Is there a way around this? Thanks so much for any help!我将所有字段都设置为必填字段,因为如果它不是必填字段,自动对焦功能似乎不起作用(即在当前字段中按 Enter 时不会自动进入下一个输入字段。有没有办法解决这个问题?谢谢非常感谢您的帮助!

 window.setTimeout(() => this.submit(), 120000)
 <html> <main> <form> <br><label for="response1"><b>Animals</b></label><br> <input type="text" id="response1" name="response1" autocomplete="off" autofocus required></br> <br><input type="text" id="response2" name="response2" autocomplete="off" autofocus required></br> <br><input type="text" id="response3" name="response3" autocomplete="off" autofocus required></br> <br><input type="text" id="response4" name="response4" autocomplete="off" autofocus required></br> <button type="submit">Submit</button> </form> </main> </html>

Sure, just put this logic in your function.当然,只需将此逻辑放在您的函数中即可。 You just remove required attribute from fields.您只需从字段中删除required属性。

let form = document.querySelector('form');
let inputs = form.getElementsByTagName('input');
let i;

for (i = 0; i < inputs.length; i++) {
    inputs[i].required = false;
}

there are a couple problems with your code:您的代码有几个问题:

  1. you should not open and close br tags, you just put a <br> where you want the line break你不应该打开和关闭 br 标签,你只需在你想要换行的地方放一个<br>
  2. the autofocus attribute should only be placed on one input, and that input will have the focus when the page loads. autofocus 属性应该只放在一个输入上,并且在页面加载时该输入将获得焦点。
  3. depending on how you are calling your code, you might run in to problems with the this keyword, you might be better calling the form directly.根据您调用代码的方式,您可能会遇到this关键字的问题,您最好直接调用表单。

I changed those things in your code and succeeded with the following code (no need to remove the required attributes, but if they are not really needed just remove them):我在您的代码中更改了这些内容并使用以下代码成功(无需删除必需的属性,但如果真的不需要它们,只需删除它们):

 window.setTimeout(() => document.forms[0].submit(), 1000)
 <html> <main> <form> <br> <label for="response1"> <b>Animals</b> </label> <br> <input type="text" id="response1" name="response1" autocomplete="off" autofocus required> <br> <input type="text" id="response2" name="response2" autocomplete="off" required> <br> <input type="text" id="response3" name="response3" autocomplete="off" required> <br> <input type="text" id="response4" name="response4" autocomplete="off" required> <button type="submit">Submit</button> </form> </main> </html>

I changed the timeout to one second just to be able to see the effect.我将超时更改为一秒只是为了能够看到效果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果未填写必填字段,如何防止提交表单上的函数调用? - How to prevent function call on submit form if required fields are not filled out? GTM - 表单提交时的Fire标记(仅当填写了必填字段时) - GTM - Fire tag on form submit (only if required fields are filled) 如何检查表单中所有必填字段的填写时间? - How can I check when all of the required fields of a form has been filled out? 使用Redux表单,如果未填写任何字段,如何禁用提交按钮? - Using Redux Form, how can I disable the submit button if no fields have been filled out? 如何检查html表单中的必填字段是否已填写? - How do I check if the required fields in an html form are filled? 当未填写必填字段时,不调用角提交 - Angular submit not called when required fields not filled 如何仅在使用 bootstrap 填写所有必需的 attr 字段后才提交表单? - How to submit a form only if all the required attr fields are filled using bootstrap? 如何强制AngluarJS表单实现必需的字段已通过DOM操作填充(无用户输入)? - How can I force an AngluarJS form to realize required fields have been filled through DOM manipulation (no user input)? 在填写动态创建的必填字段之前,请禁用提交按钮 - Keeping submit button disabled until dynamically created required fields are filled 要么不能提交(); jQuery中的表单,或者无法检查输入字段是否已填写 - Either Can Not submit(); form in jQuery, or can not check if input fields are filled out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM