繁体   English   中英

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

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

我想知道是否可以在一段时间(例如 2 分钟)后提交表单,即使没有填写所有必填字段,因为我希望提交该固定时间段内输入的所有数据. 目前,虽然我使用的是基于 javascript 的超时函数,但它不允许在超时时提交表单,因为未完成必填字段。 我将所有字段都设置为必填字段,因为如果它不是必填字段,自动对焦功能似乎不起作用(即在当前字段中按 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>

当然,只需将此逻辑放在您的函数中即可。 您只需从字段中删除required属性。

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

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

您的代码有几个问题:

  1. 你不应该打开和关闭 br 标签,你只需在你想要换行的地方放一个<br>
  2. autofocus 属性应该只放在一个输入上,并且在页面加载时该输入将获得焦点。
  3. 根据您调用代码的方式,您可能会遇到this关键字的问题,您最好直接调用表单。

我在您的代码中更改了这些内容并使用以下代码成功(无需删除必需的属性,但如果真的不需要它们,只需删除它们):

 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>

我将超时更改为一秒只是为了能够看到效果。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM