[英]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:您的代码有几个问题:
<br>
where you want the line break<br>
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.