如何检查引导模式是否打开,以便我可以使用 jquery 验证? [英] How to check if bootstrap modal is open, so I can use 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
// or, with the optional chaining operator (?.)
$("element").data('bs.modal')?._isShown // Bootstrap 4
$("element").data('bs.modal')?.isShown // Bootstrap <= 3
如Twitter Bootstrap Modal - 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
这篇关于如何检查引导模式是否打开,以便我可以使用 jquery 验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!