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.

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>

推荐答案

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

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

// or, with the optional chaining operator (?.)
$("element").data('bs.modal')?._isShown    // Bootstrap 4
$("element").data('bs.modal')?.isShown     // Bootstrap <= 3

Twitter Bootstrap Modal - 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

Jquery相关问答推荐

使用 howler.js 中的变量播放多种声音

加入了两个 select 语句,但数据显示了两次而不是一次

子 li 元素的点击事件

我需要在上一个表格单元格中显示计算结果

jQuery在localStorage时将类添加到正文

如何在外部JS文件中使用带有参数的laravel路由

jQueryUI 模态对话框不显示关闭按钮 (x)

Javascript 仅打印 iframe 内容

如何使用 jQuery 禁用粘贴(Ctrl+V)?

输入类型=文件的jQuery更改方法

聚焦 时防止 iphone 默认键盘

Jquery .show() 不显示隐藏可见性的 div

从附加元素获取 jQuery 对象的更简单方法

jquery变量语法

如何在页面加载时启动 jQuery Fancybox?

删除 Javascript 中的所有多个空格并替换为单个空格

通过 :not 在 jQuery Select 器中隐藏除 $(this) 之外的所有内容

在 jquery 中使用 AJAX Post 从强类型 MVC3 视图传递模型的正确方法

Ajax 处理中的无效 JSON 原语

如何在 jquery 中获取 textarea 的值?