简体   繁体   中英

Fire the same modal with different content at page load

I have a modal, which I feed its content information through variables.

<div id="myModal">
 <div id="outer">
  <div id="inner">
   <div id="top">Headline</div>
    <span><img class="btnClose bCancel" src="#"></span>
   <div class="modalCnt"></div>
    <div class="btn">
     <span class="btnText">OK</span>
    </div>
  </div> <!-- Inner -->
 </div> <!-- Outer -->
</div> <!-- Close myModal -->

My problem is that I use the same modal for all different messages that needs to popup. And at page load I have to messages that needs to popup, but only one shows up, which is the last modal that is called. Is there a way to que up the calls so that first modal is shown and then the second one?

$( document ).ready(function() {
    modalHead.html("<h3>Headline 1</h3>");
    modalContent.html("<p>Content 1</p>");
    modal.show();

    modalHead.html("<h3>Headline 2</h3>");
    modalContent.html("<p>Content 2</p>");
    modal.show();
});

Please revisit the following official documentation:

https://v4-alpha.getbootstrap.com/components/modal/#varying-modal-content

It is quite clear and elegant solution.

<script>
$('#myModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget)
  var header = button.data('header')
  var content = button.data('content')
  var modal = $(this)
  modal.find('.modal-title').text(header)
  modal.find('.modal-modalCnt').val(content)
})
</script>

And pass data-header="your_header" data-content="your_content" when you trigger the modal.

Plus add a class tittle to your header. Hope I was clear.

what you are doing is calling the same function with 2 diffrent modal data at once . So if we go by the flow of function , it will finish the execution at the last line. So thats why its showing last modal, So what you can do is use if - else condition,

if modal1 {
modalHead.html("<h3>Headline 1</h3>");
modalContent.html("<p>Content 1</p>");
modal.show();
}
else if modal 2 {
modalHead.html("<h3>Headline 2</h3>");
modalContent.html("<p>Content 2</p>");
modal.show();
}
else {
//default part
}

Try:

function new_modal(head, content){

    var random = Math.floor(1000 + Math.random() * 9000);

    var modal_html = '<div id="myModal_'+ random +'">' +
     '<div id="outer">' +
      '<div id="inner">' +
       '<div id="top">'+head+'</div>' +
        '<span><img class="btnClose bCancel" src="#"></span>' +
       '<div class="modalCnt">'+content+'</div>' +
        '<div class="btn">' +
         '<span class="btnText">OK</span>' +
        '</div>' +
      '</div> <!-- Inner -->' +
     '</div> <!-- Outer -->' +
    '</div>';

    $('body').append(modal_html);

    return 'myModal_' + random;

}

$( document ).ready(function() {
    modal1 = new_modal("<h3>Headline 1</h3>", "<p>Content 1</p>");
    $('#' + modal1).show();
    modal2 = new_modal("<h3>Headline 2</h3>", "<p>Content 2</p>");
    $('#' + modal2).show();
});

Hope it Helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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