Bootstrap自定义表单验证提交时如何定位未填写字段?
解决Bootstrap自定义验证自动跳转到未填写字段的问题
我懂你遇到的痛点——原生表单提交时会自动跳转到第一个未填写的必填字段,但Bootstrap的自定义验证脚本默认只添加了样式提示,没带上这个自动聚焦的功能。别担心,咱们只需要给验证脚本加几行代码就能搞定!
问题根源
Bootstrap的自定义验证脚本里,当表单验证不通过时调用了event.preventDefault()和event.stopPropagation(),这阻止了浏览器原生的表单提交行为,包括自动聚焦到无效字段的功能。所以我们需要手动实现这个聚焦逻辑。
解决方案
修改你的验证脚本,在标记表单为was-validated之前,找到第一个无效的表单元素并让它获得焦点。这里我们可以用:invalid选择器来定位无效元素,然后调用focus()方法。
修改后的完整代码
JavaScript部分
// Example starter JavaScript for disabling form submissions if there are invalid fields (function() { 'use strict'; window.addEventListener('load', function() { // Fetch all the forms we want to apply custom Bootstrap validation styles to var forms = document.getElementsByClassName('needs-validation'); // Loop over them and prevent submission var validation = Array.prototype.filter.call(forms, function(form) { form.addEventListener('submit', function(event) { if (form.checkValidity() === false) { event.preventDefault(); event.stopPropagation(); // 新增:找到第一个无效元素并聚焦 const firstInvalidElement = form.querySelector(':invalid'); if (firstInvalidElement) { firstInvalidElement.focus(); // 可选:平滑滚动到元素位置,提升用户体验 firstInvalidElement.scrollIntoView({ behavior: 'smooth', block: 'center' }); } } form.classList.add('was-validated'); }, false); }); }, false); })();
HTML部分(补全你提供的截断代码)
<html> <head> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous"> </head> <body> <form class="needs-validation" novalidate> <div class="form-row"> <div class="col-md-4 mb-3"> <label for="validationCustom01">First name</label> <input type="text" class="form-control" id="validationCustom01" placeholder="First name" value="Mark" required> <div class="valid-feedback">Looks good!</div> </div> <div class="col-md-4 mb-3"> <label for="validationCustom02">Last name</label> <input type="text" class="form-control" id="validationCustom02" placeholder="Last name" value="Otto" required> <div class="valid-feedback">Looks good!</div> </div> <div class="col-md-4 mb-3"> <label for="validationCustomUsername">Username</label> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text" id="inputGroupPrepend">@</span> </div> <input type="text" class="form-control" id="validationCustomUsername" placeholder="Username" aria-describedby="inputGroupPrepend" required> <div class="invalid-feedback">Please choose a username.</div> </div> </div> </div> <div class="form-row"> <div class="col-md-6 mb-3"> <label for="validationCustom03">City</label> <input type="text" class="form-control" id="validationCustom03" placeholder="City" required> <div class="invalid-feedback">Please provide a valid city.</div> </div> <div class="col-md-3 mb-3"> <label for="validationCustom04">State</label> <input type="text" class="form-control" id="validationCustom04" placeholder="State" required> <div class="invalid-feedback">Please provide a valid state.</div> </div> <div class="col-md-3 mb-3"> <label for="validationCustom05">Zip</label> <input type="text" class="form-control" id="validationCustom05" placeholder="Zip" required> <div class="invalid-feedback">Please provide a valid zip.</div> </div> </div> <div class="form-group"> <div class="form-check"> <input class="form-check-input" type="checkbox" value="" id="invalidCheck2" required> <label class="form-check-label" for="invalidCheck2">Agree to terms and conditions</label> <div class="invalid-feedback">You must agree before submitting.</div> </div> </div> <button class="btn btn-primary" type="submit">Submit form</button> </form> </body> </html>
效果说明
现在当你提交表单时:
- 表单会阻止默认提交行为,保留Bootstrap的自定义验证样式(红色边框、错误提示)
- 页面会自动跳转到第一个未填写/不符合要求的字段,并让它获得焦点,和原生表单的行为完全一致
内容的提问来源于stack exchange,提问作者Keerthi Reddy Yeruva
相关产品推荐
相关产品推荐

