简体   繁体   English

如果所有字段都填写在 jQuery 中而不使用each(),如何自动提交表单?

[英]How to automatically submit a form if all fields are filled in jQuery without using each()?

I have made a OTP form which I want to submit via AJAX if all it's input fields are filled in. Is it possible with using class selector $(".tp") ?我已经制作了一个 OTP 表单,如果它的所有输入字段都已填写,我想通过 AJAX 提交。是否可以使用 class 选择器$(".tp")

 $(document).ready(function() { $(".tp").keyup(function() { if (this.value.length == 1) { // this will automatically change the focus as soon as an input is done $(this).next().focus() } }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="tel" class="tp" maxlength="1"> <input type="tel" class="tp" maxlength="1"> <input type="tel" class="tp" maxlength="1"> <input type="tel" class="tp" maxlength="1">

To achieve this without an explicit loop you can use filter() to find all fields which do not have a value.要在没有显式循环的情况下实现这一点,您可以使用filter()来查找所有没有值的字段。 If there aren't any, then you know you are safe to make your AJAX request.如果没有,那么您知道您可以安全地提出 AJAX 请求。 Try this:尝试这个:

 jQuery($ => { let $tp = $(".tp").on('input', e => { if (e.target.value.length == 1) { $(e.target).next().focus(); } if ($tp.filter((i, el) =>.el.value.trim()).length == 0) { console.log('AJAX call goes here..;'); } }); });
 input { width: 15px; text-align: center; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="tel" class="tp" maxlength="1"> <input type="tel" class="tp" maxlength="1"> <input type="tel" class="tp" maxlength="1"> <input type="tel" class="tp" maxlength="1">

暂无
暂无

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

相关问题 禁用表单提交按钮,直到填充所有输入/文本区域字段,而不使用 for 循环(无 jQuery) - Disable form submit button until all input/textarea fields are filled, without using for loop (no jQuery) 如何仅在使用 bootstrap 填写所有必需的 attr 字段后才提交表单? - How to submit a form only if all the required attr fields are filled using bootstrap? 填写完所有字段后如何提交JavaScript - How to submit for after all the fields are filled in JavaScript jQuery确保填写所有表单字段 - jQuery make sure all form fields are filled 如何在表单上提交操作(<form></form> ) 在完成/填写此表单中的所有输入元素后自动触发? - How can the submit action on a form(<form></form>) be automatically fired when all input elements within this form are completed/filled? 在 ReactJS 中填写所有输入字段之前,如何禁用表单提交按钮? - How to disable form submit button until all input fields are filled in ReactJS? 如何在填写所有输入字段之前禁用表单提交按钮?! ReactJS ES2015 - How to disable form submit button until all input fields are filled?! ReactJS ES2015 如何检查表格的所有字段是否都已填写? - How to check if all the fields of a form are filled? Vue如何查看表单中的所有字段是否已填写 - How to see if all fields are filled in a form with Vue 使用Redux表单,如果未填写任何字段,如何禁用提交按钮? - Using Redux Form, how can I disable the submit button if no fields have been filled out?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM