如何检查bootstrap modal是否打开,所以我可以使用jquery validate?

问题描述:

我只需要在模式打开的情况下进行验证,因为如果我先打开它,然后将其关闭,则我按下打开模式的按钮将不起作用,因为它正在进行jquery验证,但未显示,因为该模式已被撤消.

I need to make a validation only if a modal is open, because if I open it, and then I close it, and the I press the button that opens the modal it doesn't work because it is making the jquery validation, but not showing because the modal was dismissed.

因此,如果模式已打开,我想广告一个jQuery,以便我进行验证,这可能吗?

So I want to ad a jquery if modal is open so the i do validate, is this possible?

<script>
$(document).ready(function(){

var validator =$('#form1').validate(
 {
  ignore: "",
  rules: {

usu_login: {
  required: true
},
usu_password: {
  required: true
},
usu_email: {
  required: true
},
usu_nombre1: {
  required: true
},
usu_apellido1: {
  required: true
},
usu_fecha_nac: {
  required: true
},
usu_cedula: {
  required: true
},
usu_telefono1: {
  required: true
},
rol_id: {
  required: true
},
dependencia_id: {
  required: true
},
  },

  highlight: function(element) {
$(element).closest('.grupo').addClass('has-error');
        if($(".tab-content").find("div.tab-pane.active:has(div.has-error)").length == 0)
        {
            $(".tab-content").find("div.tab-pane:hidden:has(div.has-error)").each(function(index, tab)
            {
                var id = $(tab).attr("id");
        
                $('a[href="#' + id + '"]').tab('show');
            });
        }
  },
  unhighlight: function(element) {
$(element).closest('.grupo').removeClass('has-error');
  }
 });

}); // end document.ready

</script>

为避免@GregPettit提到的竞争条件,可以使用:

To avoid the race condition @GregPettit mentions, one can use:

($("element").data('bs.modal') || {})._isShown    // Bootstrap 4
($("element").data('bs.modal') || {}).isShown     // Bootstrap <= 3

,如 Twitter引导程序模式-IsShown 中所述.

当尚未打开模态时,.data('bs.modal')返回undefined,因此返回|| {}-这将使isShown成为(虚假的)值undefined.如果您严格要求的话,可以做($("element").data('bs.modal') || {isShown: false}).isShown

When the modal is not yet opened, .data('bs.modal') returns undefined, hence the || {} - which will make isShown the (falsy) value undefined. If you're into strictness one could do ($("element").data('bs.modal') || {isShown: false}).isShown