我正在使用,Bootstrap 4并且如果以下脚本中的任何字段无效,我将停止表单提交。我试图弄清楚(到目前为止没有成功)我需要在“ event.stopPropagation();” 之后添加什么代码,以使表单滚动到找到的第一个无效字段。感谢您的帮助,谢谢。
形成:
<form class="needs-validation" novalidate action="search.php" id="firstform" method="post" >
Run Code Online (Sandbox Code Playgroud)
如果无效则阻止提交:
<script>
// 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();
}
form.classList.add('was-validated'); …Run Code Online (Sandbox Code Playgroud)