简体   繁体   English

如何通过jQuery正确关闭模式

[英]how to properly close a modal through jQuery

Here is the jquery 这是jQuery

$(document).ready(function(){
$(".btn-add-menu").click(function(){
  $('.pace-done').addClass('no-pad');
  $("#myModalAddApplet").modal('hide');
});

});

The issue with this is that it successfully hides the modal #myModalAddApplet but the new modal it opens are long and the scroll bar is not working with the modal after this happens, i have tried debugging it myself and the issue is with the line $("#myModalAddApplet").modal('hide'); 问题是它成功地隐藏了模态#myModalAddApplet但是它打开的新模态很长,并且在这种情况发生后滚动条不能与模态一起工作,我已经尝试自己调试了,问题是$("#myModalAddApplet").modal('hide'); because if I comment out that line then the scroll bar works fine. 因为如果我注释掉该行,则滚动条可以正常工作。

Anyone here who can teach me how to properly close or hide my modal... 可以教我如何正确关闭或隐藏我的模态的任何人...

PS. PS。 .btn-add-menu opens another modal... .btn-add-menu打开另一个模式...

Your problem is not in the jQuery code (it is correct), but in the fact that you're forcing jQuery/Bootstrap to close & open the dialog at the same time. 您的问题不在jQuery代码中(这是正确的),但实际上您正在强迫jQuery / Bootstrap同时关闭和打开对话框。 You really shouldn't bind two actions (both declarative and imperative, in particular) on a single button - this is an antipattern & is overall a bad programming habit. 您实际上不应该在单个按钮上绑定两个动作(特别是声明式和命令式的动作)-这是一种反模式,总体上是一种不良的编程习惯。

Remove the declarative dialog call from the .btn-add-menu , move it to the jQuery action and wrap in a timeout to allow previous dialog action to complete: .btn-add-menu删除声明性对话框调用,将其移至jQuery操作并包装一个超时以允许先前的对话框操作完成:

$(".btn-add-menu").click(function(){
    $('.pace-done').addClass('no-pad');
    $("#myModalAddApplet").modal('hide');

    setTimeout(function() {
        $("#otherApplet").modal('show');
    },
    500);
});

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

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