简体   繁体   English

在表单提交时关闭 Bootstrap 模式

[英]Close Bootstrap modal on form submit

I have a Bootstrap modal dialog which contains a form.我有一个包含表单的 Bootstrap 模式对话框。 The modal dialog contains a submit and a cancel button.模态对话框包含一个提交和一个取消按钮。 Now on submit button click the form is submitted successfully but the modal dialog isn't getting closed.现在在submit按钮上单击表单已成功提交但模态对话框没有关闭。 Here is my HTML:这是我的 HTML:

<div class="modal fade" id="StudentModal" tabindex="-1" role="dialog" aria-labelledby="StudentModalLabel" aria-hidden="true" data-backdrop="static">
   <div class="modal-dialog">
      <div class="modal-content">
         <form action="~/GetStudent" class="form-horizontal" role="form" method="post" id="frmStudent">
            <div class="modal-footer">
               <div class="pull-right">
                  <button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-ok"></i> Save</button>
                  <button type="button" class="btn btn-danger" data-dismiss="modal"><i class="glyphicon glyphicon-remove"></i> Close</button>
               </div>
            </div>
         </form>
      </div>
   </div>

Anybody knows how to do this?有谁知道如何做到这一点?

Use that Code使用该代码

 $('#button').submit(function(e) {
    e.preventDefault();
    // Coding
    $('#IDModal').modal('toggle'); //or  $('#IDModal').modal('hide');
    return false;
});

Add the same attribute as you have on the Close button:添加与关闭按钮相同的属性:

data-dismiss="modal"

eg例如

<button type="submit" class="btn btn-success" data-dismiss="modal"><i class="glyphicon glyphicon-ok"></i> Save</button>

You can also use jQuery if you wish:如果您愿意,也可以使用 jQuery:

$('#frmStudent').submit(function() {

    // submission stuff

    $('#StudentModal').modal('hide');
    return false;
});

give id to submit button给 id 提交按钮

<button id="btnSave" type="submit" class="btn btn-success"><i class="glyphicon glyphicon-trash"></i> Save</button>

$('#btnSave').click(function() {
   $('#StudentModal').modal('hide');
});

Also you forgot to close last div.你也忘了关闭最后一个 div。

</div>

Hope this helps.希望这可以帮助。

You can use one of this two options:您可以使用以下两个选项之一:

1) Add data-dismiss to the submit button ie 1)在提交按钮上添加数据关闭即

<button type="submit" class="btn btn-success" data-dismiss="modal"><i class="glyphicon glyphicon-ok"></i> Save</button>

2) Do it in JS like 2)用JS做

$('#frmStudent').submit(function() {
    $('#StudentModal').modal('hide');
});

i make this code it work fine but once form submit model button not open model again say e.preventdefault is not a function..我让这段代码可以正常工作,但是一旦表单提交模型按钮不再打开模型,就说 e.preventdefault 不是一个功能..

Use below code on any event that fire on submit form在提交表单上触发的任何事件上使用以下代码

                $('.modal').removeClass('in');
                $('.modal').attr("aria-hidden","true");
                $('.modal').css("display", "none");
                $('.modal-backdrop').remove();
                $('body').removeClass('modal-open');

you can make this code more short..你可以让这段代码更短..

if any body found solution for e.preventdefault let us know如果任何机构找到了 e.preventdefault 的解决方案,请告诉我们

I am using rails and ajax too and I had the same problem.我也在使用 rails 和 ajax,我也遇到了同样的问题。 just run this code只需运行此代码

 $('#modalName').modal('hide');

that worked for me but I am trying to also fix it without using jQuery.这对我有用,但我也在尝试在不使用 jQuery 的情况下修复它。

If anyone does not wish to use jQuery, In Bootsrap 5.0, you should now use data-bs-dismmis eg :如果有人不想使用 jQuery,在 Bootsrap 5.0 中,您现在应该使用data-bs-dismmis例如:

<button type="submit" class="btn btn-success" data-bs-dismiss="modal">SUBMIT</button>

or或者

<button type="button" class="btn btn-success" onclick="submitForm()" data-bs-dismiss="modal">SUBMIT</button>

Neither adding data-dismiss="modal" nor using $("#modal").modal("hide") in javascript worked for me.在 javascript 中添加data-dismiss="modal"或使用$("#modal").modal("hide")都不适合我。 Sure they closed the modal but the ajax request is not sent either.当然他们关闭了模式,但也没有发送 ajax 请求。

Instead, I rendered the partial containing the modals again after ajax is successful (I'm using ruby on rails).相反,我在 ajax 成功后再次渲染了包含模态的部分(我在 rails 上使用 ruby​​)。 I also had to remove "modal-open" class from the body tag too.我还必须从 body 标签中删除"modal-open"类。 This basically resets the modals.这基本上重置了模态。 I think something similar, with a callback upon successful ajax request to remove and add back the modals should work in other framework too.我认为类似的事情,在成功的 ajax 请求后回调以删除和添加模式也应该在其他框架中工作。

if your modal has a cancel button (Otherwise create a hidden close button).如果您的模态有一个取消按钮(否则创建一个隐藏的关闭按钮)。 You can simulate the click event on this button so that your form is closed.您可以模拟此按钮上的单击事件,以便关闭您的表单。 Iin your component add a ViewChild在你的组件中添加一个ViewChild

export class HelloComponent implements OnInit {

@ViewChild('fileInput') fileInput:ElementRef;

in your close button add #fileInput在您的关闭按钮中添加#fileInput

<button class="btn btn-danger" #fileInput id="delete-node" name="button" data-dismiss="modal" type="submit">Cancelar</button>

When you want to close the modal programmatically trigger an click event on the close button当您想以编程方式关闭模态时触发关闭按钮上的点击事件

this.fileInput.nativeElement.click();

I had the same problem and finally got it working with this code:我遇到了同样的问题,终于用这段代码让它工作了:

<%=form_with id: :friend_email_form, url: friend_emails_create_path do |f|%>

                      # form fields entered here

<div class="actions">
    <%= f.submit "Send Email", class: 'btn btn-primary', "onclick":"submit_form();", "data-dismiss":"modal"%>
</div>

 <script>
    function submit_form() {
      document.getElementById('friend_email_form').submit();
    }
</script>

The selected answer did not work for me.所选答案对我不起作用。

The listed answers won't work if the results of the AJAX form submit affects the HTML outside the scope of the modal before the modal finishes closing.如果 AJAX 表单提交的结果在模态完成关闭之前影响模态范围之外的 HTML,则列出的答案将不起作用。 In my case, the result was a grayed out (fade) screen where I could see the page update, but could not interact with any of the page elements.就我而言,结果是一个灰显(淡入淡出)屏幕,我可以在其中看到页面更新,但无法与任何页面元素进行交互。

My solution:我的解决方案:

$(document).on("click", "#send-email-submit-button", function(e){
    $('#send-new-email-modal').modal('hide')
});

 $(document).on("submit", "#send-email-form", function(e){
    e.preventDefault();
    var querystring = $(this).serialize();
    $.ajax({
        type: "POST",
        url: "sendEmailMessage",
        data : querystring,
        success : function(response) {
            $('#send-new-email-modal').on('hidden.bs.modal', function () {
                $('#contacts-render-target').html(response);
            }
    });
    return false;
});

Note: My modal form was inside a parent div that was already rendered via AJAX, thus the need to anchor the event listener to document.注意:我的模态表单位于已经通过 AJAX 呈现的父 div 中,因此需要将事件侦听器锚定到文档。

Try this code试试这个代码

$('#frmStudent').on('submit', function() {
  $(#StudentModal).on('hide.bs.modal', function (e) {
    e.preventDefault();
  })
});

Use this to submit and close the modal at the same time使用这个同时提交和关闭模态

$('#form-submit').on('click', function(e){
    e.preventDefault();
    $('#con-close-modal').modal('toggle'); //or  $('#IDModal').modal('hide');
    $('#date-form').submit();
});

If you do not want to use jQuery, make the button type a normal button and add a click listener pointing to the function you would like to execute, and send the form in as a parameter.如果您不想使用 jQuery,请将按钮类型设为普通按钮并添加指向您要执行的函数的单击侦听器,并将表单作为参数发送。 The button would be as follows:该按钮如下所示:

<button (click)="yourSubmitFunction(yourForm)" [disabled]="!yourForm.valid" type="button" class="btn btn-primary" data-dismiss="modal">Save changes</button>

Remember to remove: (ngSubmit)="yourSubmitFunction(yourForm)" from the form div if you use this method.如果您使用此方法,请记住从表单 div 中删除: (ngSubmit)="yourSubmitFunction(yourForm)"

This worked for me.这对我有用。 I embedded an inline JavaScript code in the submit button to auto-click the close button on submit.我在提交按钮中嵌入了一个内联 JavaScript 代码,以在提交时自动单击关闭按钮。 I used the ID of the close button to reference it from the javascript (jQuery).我使用关闭按钮的 ID 从 javascript (jQuery) 中引用它。

<button type="submit" class="btn btn-success float-right" onclick="javascript: $('#close-button').click();">Save</button>

What I do is press the close button that automatically brings the modal to the moment the request is satisfactorily fulfilled.was what I came up with at that time and it worked for me, I am using laravel with axios我所做的是按下关闭按钮,自动将模态带到请求令人满意地满足的那一刻。这是我当时想出的并且对我有用,我正在使用带有 axios 的 laravel

i add an ID in the close button of modal and after i select the buttton and execute when the response is correct我在模态的关闭按钮中添加一个 ID,然后在我选择按钮并在响应正确时执行

document.getElementById("closeGroup").click();

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

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