最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery .load() bootstrap modal and show - Stack Overflow

programmeradmin4浏览0评论

I am trying to .load() a bootstrap modal in my page and show it immediately. Below is the code I have written (which isn't working as the modal opens but can then never be closed)!

Basically I am trying to .load() a list of players in a modal when you click on a team name (with the class .team), I've tried various ways of calling $($this).empty() with no success.

Here is my function:

   $(function () {
        $('.team').on('click', function () {
            var target = $(this).data('target');

            $('#results-modals').load('/team-players.html ' + target, function (response, status, xhr) {
                if (status === "success") {
                    $(target).modal({ show: true });
                }
            });
        });
  });

Here is the html anchor and example modal:

<a class='team' data-toggle='modal' data-target='#20'>Real Madrid</a>

Data held in external file team-players.html:

<div id='20' class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
     <div class="modal-content">
       <p>Player 1: Ronaldo</p>
    </div>
  </div>
</div>

I am trying to .load() a bootstrap modal in my page and show it immediately. Below is the code I have written (which isn't working as the modal opens but can then never be closed)!

Basically I am trying to .load() a list of players in a modal when you click on a team name (with the class .team), I've tried various ways of calling $($this).empty() with no success.

Here is my function:

   $(function () {
        $('.team').on('click', function () {
            var target = $(this).data('target');

            $('#results-modals').load('/team-players.html ' + target, function (response, status, xhr) {
                if (status === "success") {
                    $(target).modal({ show: true });
                }
            });
        });
  });

Here is the html anchor and example modal:

<a class='team' data-toggle='modal' data-target='#20'>Real Madrid</a>

Data held in external file team-players.html:

<div id='20' class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
     <div class="modal-content">
       <p>Player 1: Ronaldo</p>
    </div>
  </div>
</div>
Share Improve this question edited Sep 5, 2017 at 12:38 Matt D. Webb asked Aug 4, 2014 at 23:24 Matt D. WebbMatt D. Webb 3,3144 gold badges31 silver badges51 bronze badges 3
  • That returns a 'Uncaught ReferenceError: show is not defined' – Matt D. Webb Commented Aug 4, 2014 at 23:28
  • Rename your variable to anything else!, that doesn't start with $. – Kosh Commented Aug 4, 2014 at 23:30
  • 1 And use $(this).attr('data-target') instead of data – Kosh Commented Aug 4, 2014 at 23:31
Add a comment  | 

4 Answers 4

Reset to default 8

$(function(){
  var url = "url of the modal window content";
  $('.my-modal-cont').load(url,function(result){
  $('#myModal').modal({show:true});
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<!-- Modal Window Container -->
<div class="my-modal-base">
	<div class="my-modal-cont"></div>
</div>

<!-- Modal Window content -->
<div id="myModal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog">
	<div class="modal-content">
		<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal">×</button>
			<h3>Modal header</h3>
	</div>
	<div class="modal-body">
		<p>My modal content here…</p>
	</div>
	<div class="modal-footer">
		<button class="btn" data-dismiss="modal">Close</button>
	</div>
	</div>
</div>
</div>

Instead of $($this).modal({ show: true }); try $(this).modal('show');

To hide the modal, you can use $(this).modal('hide');

You can do $(this).modal("toggle") if it open it will close and vice versa.

jQuery .load() bootstrap modal and show

for show $(#ID).modal('show');

for hide $(#ID).modal('hide');

发布评论

评论列表(0)

  1. 暂无评论