简体   繁体   English

Bootstrap 4 表单验证和提交

[英]Bootstrap 4 form validation & submission

I have a Bootstrap 4 form using validation, using the "starter" javascript found on their web site: https://getbootstrap.com/docs/4.4/components/forms/#validation .我有一个使用验证的 Bootstrap 4 表单,使用在他们的 web 站点上找到的“starter”javascript: https://getbootstrap.com//#idation/4

This form exists inside of a Google Docs add-on sidebar.此表单存在于 Google Docs 附加边栏中。 When the form submits, I'm not calling another URI, but rather, I want to only collect the information from the form and then work with it using Google Apps Script.当表单提交时,我不会调用另一个 URI,而是我只想从表单中收集信息,然后使用 Google Apps 脚本处理它。

The starter javascript is correctly validating the form fields, but when the form submits, a new browser tab is opening, which I DON'T want to happen.启动器 javascript 正在正确验证表单字段,但是当表单提交时,一个新的浏览器选项卡正在打开,我不想发生这种情况。

I've modified the starter code below only to call my own function from within the 'else' portion.我已经修改了下面的启动代码,只是为了从“其他”部分调用我自己的 function。 Currently, my function submitFeedback() does nothing more than post an alert to let me know code execution has reached that point, but at the same time execution transfers to function submitFeedback(), the new tab opens in the browser.目前,我的 function submitFeedback() 只是发布一个警报,让我知道代码执行已达到该点,但同时执行转移到 function submitFeedback(),新选项卡在浏览器中打开。

How can I prevent this new browser tab from opening when the form is submitted?提交表单时,如何防止打开这个新的浏览器选项卡?

<form class="needs-validation" novalidate>
    <div class="form-group">
        <label for="frmFirstName">First name</label>
        <input type="text" class="form-control" id="frmFirstName" placeholder="First name" required>
        <div class="invalid-feedback">
            Please enter your first name.
        </div>
    </div>
    <div class="form-group">
        <label for="frmLastName">Last name</label>
        <input type="text" class="form-control" id="frmLastName" placeholder="Last name" required>
        <div class="invalid-feedback">
            Please enter your last name.
        </div>
    </div>
    <div class="form-group">
        <label for="frmEmail">Email</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="frmEmail" placeholder="user@domain.com" aria-describedby="inputGroupPrepend" required>
            <div class="invalid-feedback">
                Please enter your email.
            </div>
        </div>
    </div>                          
    <div class="form-group">
        <label for="frmCategory">What's on your mind?</label>
        <select id="frmCategory" class="custom-select form-control" required>
            <option value="">- Select -</option>
            <option value="1">I have a question</option>
            <option value="2">I have an idea</option>
            <option value="3">I have a compliment</option>
            <option value="4">I found a problem :(</option>
        </select>
        <div class="invalid-feedback">
            Please select an option.
        </div>
    </div>
    <div class="form-group">
        <label for="frmDetails">Tell us all about it...</label>
        <textarea id="frmDetails" class="form-control" rows="3" placeholder="Enter your comments here" required></textarea>
        <div class="invalid-feedback">
            Please provide your comments here.
        </div>
    </div>                               
    <button class="btn btn-primary btn-sm" type="submit">Submit form</button>
</form>


...

<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();
        } else {
        submitFeedback();
        }
        form.classList.add('was-validated');
      }, false);
    });
  }, false);
})();

// function to gather and process data from form
function submitFeedback(){
    alert ('inside feedback function');
}
</script>

In Firefox latest and Chrome its not opening a new tab at least not the code posted.So check if you use a local webserver what is defined there or if using an other browser if its a browser specific behavior.在最新的 Firefox 和 Chrome 中,它没有打开新标签,至少没有发布的代码。因此,请检查您是否使用本地网络服务器定义的内容,或者是否使用其他浏览器,如果它是浏览器特定的行为。 If its the same browser, try to empty cache and open developer tools to see whats happening in detail.如果它是同一个浏览器,请尝试清空缓存并打开开发人员工具以查看详细信息。

base target in the head section of the html file was the culprit. html 文件头部的基本目标是罪魁祸首。 After removing it, all worked as anticipated.删除它后,一切都按预期工作。 This tag is added by default when creating a new html file using the Google Apps Script editor.在使用 Google Apps 脚本编辑器创建新的 html 文件时默认添加此标记。

<head> <base target="_top"> </head>

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

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