Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

How to trigger a function call in jquery when bootstrap modal hides if my modal does not have a unique id or class

Objective: I have a bootstrap modal:

  1. I want to make a function call when this modal hides.
  2. I want to call this function only once when the modal closes.

Note: I'm using Bootstrap 3.

Problem:

  1. I cannot make changes to the HTML.
  2. The HTML has dynamic class names ex:js123, js456 etc.
  3. There is no unique class name or id for the modal itself.

Sample Code:

<body class="app-body">
<div role="presentation" class="jss20" style="position: fixed; z-index: 1300; inset: 0px;">
<div aria-hidden="true" style="z-index: -1; position: fixed; inset: 0px; background-color: rgba(0, 0, 0, 0.5);"></div>
<div tabindex="0" data-test="start"></div>
<div class="jss942 modal-content" tabindex="-1">
<div class="jss946">
<div class="jss950">
<span id="newdata-modal-title">New Data 1</span>
...
</div></div></div></div></div></div></body>

I've tried the following with no luck:

$('div.modal-content').one('hide', function () {
onhideCall(param1, param2);
});
$('div.modal-content').on("hide.bs.modal", function () {
onhideCall(param1, param2);
});
$('div[role="presentation"].modal-content').click(function(){
onhideCall(param1, param2);
});
$('div[role="presentation"]').on("hide.bs.modal", function(){
onhideCall(param1, param2);
});

Another problem I am facing is that there are multiple classes with name "modal-content". That could be a problem too?. See the following chrome console output:

>>$('.modal-content')

m.fn.init(17)?[div.modal-content, div.modal-content, div.modal-content, div.modal-content, div.modal-content, div.modal-content, div.modal-content, div.modal-content, div.modal-content, div.modal-content, div.modal-content, div.modal-content, div.modal-content, div.modal-content, div.modal-content, div.modal-content, div.jss942.modal-content, prevObject: m.fn.init(1), context: document, selector: ".modal-content"]

Can someone please help me out here? Thanks very much in advance. Your help is highly appreciated.

question from:https://stackoverflow.com/questions/65928687/how-to-trigger-a-function-call-in-jquery-when-bootstrap-modal-hides-if-my-modal

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...