简体   繁体   English

Data-Dismiss:如何在 function 中关闭引导模式

[英]Data-Dismiss: How to dismiss bootstrap modal inside function

I have a modal in which I need to validate some input using knockout validation.我有一个模态,我需要在其中使用敲除验证来验证一些输入。 When I click the submit button, a function is called that validates the data.当我单击提交按钮时,将调用 function 来验证数据。 The following functionality is expected:预期具有以下功能:

  • If the validation fails, the modal stays open and the validation reason is displayed.如果验证失败,模式将保持打开状态并显示验证原因。
  • If the validation succeeds, I want to close the modal.如果验证成功,我想关闭模式。

How can I go about closing the modal inside my function?我 go 如何关闭我的 function 中的模式?

What have you tried so far? 你试过什么了?

Per the Bootstrap documentation 根据Bootstrap文档

$('#myModal').modal('hide')

Please read through the documentation! 请仔细阅读文档!
http://getbootstrap.com/javascript/#modals http://getbootstrap.com/javascript/#modals

Make another button like this 像这样制作另一个按钮

<button type="button" class="btn btn-warning btn-lg shiny" data-dismiss="modal" aria-hidden="true">Cancel</button>

This button contains data-dismiss="modal" .You can hide this button if you want. 此按钮包含data-dismiss =“modal”。如果需要,您可以隐藏此按钮。

Now You can use any other function in a customized way and when you want to hide the modal you can call 现在您可以以自定义方式使用任何其他功能,并且当您想隐藏可以调用的模态时

$(".btn-warning").click();

The above didn't work for me, modified slightly to add an id to the button and reference it that way. 以上对我来说不起作用,稍微修改为按钮添加一个id并以这种方式引用它。 I just used the cancel button that was already on my modal form and added id="cancel-btn" to that one. 我只是使用了我的模态表单上的取消按钮,并将id =“cancel-btn”添加到该模式。

<button type="button" class="btn btn-ar btn-default" id="cancel-btn" data-dismiss="modal">  

$('#cancel-btn').click();

This will only hide modal 这只会隐藏模态

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

but other modal related stuff will not remove. 但其他模态相关的东西不会删除。

To completely remove modal from page and its css 从页面及其CSS中完全删除模态

$('#modalId').modal('hide'); or $('#modalId').modal('toggle');
$('body').removeClass('modal-open');
$('body').css('padding-right', '0px');
$('.modal-backdrop').remove();
Here I'm giving a solution in plain javascript. I used Vue js and I don't want to use jQuery along with Vue.



document.querySelector('#modalid').classList.remove('show');
  document.querySelector('body').classList.remove('modal-open');
  const mdbackdrop = document.querySelector('.modal-backdrop');
  if (mdbackdrop){
    mdbackdrop.classList.remove('modal-backdrop', 'show');
  }

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

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