javascript - Ajax load with bootstrap modal - Stack Overflow

I have bootstrap modal please click to see demo and my modal has been opening automatically and closing

I have bootstrap modal please click to see demo and my modal has been opening automatically and closing automatically too and I need one more thing is ajax content and I guess it can be with load function I found some function but I couldn't apply it because I don't want to loaded clicking by link I have to loaded automatically after my modal open

I have two attribute data-open and data-close to open and close automatically my modal

$(function(){
  setTimeout(function(e){
    $('#AniPopup').modal('show');
  }, parseInt($('#AniPopup').attr('data-open')) * 1000);
  setTimeout(function(e){
    $('#AniPopup').modal('hide');
  }, parseInt($('#AniPopup').attr('data-close')) * 1000);
 
});
<link href=".3.7/css/bootstrap.min.css" rel="stylesheet"/>



<div class="modal fade" id="AniPopup" tabindex="-1" role="dialog" aria-labelledby="AniPopupLabel" aria-hidden="true"  data-close="1000" data-open="2" data-src="">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="memberModalLabel">Popup Header</h4>
      </div>
      <div class="modal-body"></div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">Kapat</button>
      </div>
    </div>
  </div>
</div>


<script src=".1.1/jquery.min.js"></script>
<script src=".3.7/js/bootstrap.min.js"></script>

I have bootstrap modal please click to see demo and my modal has been opening automatically and closing automatically too and I need one more thing is ajax content and I guess it can be with load function I found some function but I couldn't apply it because I don't want to loaded clicking by link I have to loaded automatically after my modal open

I have two attribute data-open and data-close to open and close automatically my modal

$(function(){
  setTimeout(function(e){
    $('#AniPopup').modal('show');
  }, parseInt($('#AniPopup').attr('data-open')) * 1000);
  setTimeout(function(e){
    $('#AniPopup').modal('hide');
  }, parseInt($('#AniPopup').attr('data-close')) * 1000);
 
});
<link href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>



<div class="modal fade" id="AniPopup" tabindex="-1" role="dialog" aria-labelledby="AniPopupLabel" aria-hidden="true"  data-close="1000" data-open="2" data-src="https://www.youtube.">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="memberModalLabel">Popup Header</h4>
      </div>
      <div class="modal-body"></div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">Kapat</button>
      </div>
    </div>
  </div>
</div>


<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/js/bootstrap.min.js"></script>

Share Improve this question asked Mar 15, 2017 at 12:29 ani_cssani_css 2,1263 gold badges33 silver badges77 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Bootstrap provides you with these options

$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...probably make an ajax call and get data 
})

$('#myModal').on('shown.bs.modal', function (e) {
  // do something...
})

http://getbootstrap./javascript/#modals

you call call your ajax request into this code..

$(function(){
          setTimeout(function(e){
              $.ajax({
                  type: 'GET',
                  data: {},
                  url: [Server URL],
                  success: function(response){
                      // do anything with response you want
                      $('#AniPopup').modal('show');
                      hidePopUp(); // you have to initiate hidepopup to ensure you timeout is after ajax plete not before as ajax are async call.
                  }
              })
          }, parseInt($('#AniPopup').attr('data-open')) * 1000);

            function hidePopUp(){
              setTimeout(function(e){
                $('#AniPopup').modal('hide');
              }, parseInt($('#AniPopup').attr('data-close')) * 1000);
            }
        });

This is my solution I use in every project.

Root javascript so I can open modal on every page:

$('body').on('click', '[data-toggle="modal"]', function(e) {
    var url = $(this).data('href');
    var modalId = '#'+$(this).prop('href').split('#')[1];
    if (url.indexOf('#') === 0) {
    } else {
        $(modalId+' div.modal-content').html(
            $(modalId+' div.modal-dialog img#ajax-loader').clone().removeClass('hidden')
        );
        $.get(url, function(data) {
            $(modalId+' div.modal-content').html(data) ;
        });
    }
});

Then you need this HTML in your template, I include it in base template so it is available on every page:

<div class="modal fade" role="dialog" style="display: none" id="ajax-modals">
    <!-- Fix if datepicker is used in modal -->
    <style>
        .datepicker{z-index:1151 !important;}
    </style>
    <div class="modal-dialog">
        <img src="/img/select2-spinner.gif" id="ajax-loader" class="hidden" style="margin: 20px auto; display: block;"/>
        <div class="modal-content">
            <!-- es from ajax -->

        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

To trigger the modal you need links like this:

<a data-toggle="modal" href="#ajax-modals" data-href="/ajax-url" class="btn btn-default btn-xs" >Load Modal with AJAX content</a>

And at least, this is the content your AJAX-Controller should return:

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
        ×
    </button>
    <h4 class="modal-title">
        Modal title
    </h4>
</div>
<div class="modal-body">

    Modal body goes here

    <div class="form-actions">
        <button type="button" class="btn btn-default" data-dismiss="modal">
            Close window
        </button>
    </div>
</div>
<script type="application/javascript">
    $('document').ready(function(){

        //you can do some javascript here

    });
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744669396a4586952.html

相关推荐

  • javascript - Ajax load with bootstrap modal - Stack Overflow

    I have bootstrap modal please click to see demo and my modal has been opening automatically and closing

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信