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

jquery - Bootstrap 3showing modal doesn't work with the javascript way - Stack Overflow

programmeradmin3浏览0评论

I use the Modal feature from Bootstrap 3.0.

I have this code:

<a data-toggle="modal" href="/myNestedContent" data-target="#myModal">
  Open the modal containing the content
</a>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
//nested content will be inserted here
</div>

When I click on the anchor (the link), the whole works => I see the modal with the content.

However, when I use the Javascript way (instead of the link) to show the modal like this:

$('#myModal').modal('show'); 

I only see the fade effect without the modal being shown...

When I started by clicking on the link, THEN calling the javascript, it works. (side effect?)

When I started by the javascript way, EVEN the link only shows the fade effect without the modal.

Might it be .. a bug from the modal Javascript method?

For information, the declarations of my scripts that I use are in the right order:

<script src="//code.jquery/jquery.js"></script>
<script src="javascripts/jquery.form.min.js"></script> 
<script src="javascripts/bootstrap.min.js"></script>

I use the Modal feature from Bootstrap 3.0.

I have this code:

<a data-toggle="modal" href="/myNestedContent" data-target="#myModal">
  Open the modal containing the content
</a>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
//nested content will be inserted here
</div>

When I click on the anchor (the link), the whole works => I see the modal with the content.

However, when I use the Javascript way (instead of the link) to show the modal like this:

$('#myModal').modal('show'); 

I only see the fade effect without the modal being shown...

When I started by clicking on the link, THEN calling the javascript, it works. (side effect?)

When I started by the javascript way, EVEN the link only shows the fade effect without the modal.

Might it be .. a bug from the modal Javascript method?

For information, the declarations of my scripts that I use are in the right order:

<script src="//code.jquery./jquery.js"></script>
<script src="javascripts/jquery.form.min.js"></script> 
<script src="javascripts/bootstrap.min.js"></script>
Share Improve this question edited Aug 25, 2013 at 5:38 Thaddeus Albers 4,1925 gold badges36 silver badges43 bronze badges asked Aug 24, 2013 at 22:58 Mik378Mik378 22.2k15 gold badges91 silver badges190 bronze badges 1
  • 1 Not sure what's wrong but it works for me using the method you described - jsfiddle/kZyLu – David Spence Commented Aug 25, 2013 at 1:40
Add a ment  | 

3 Answers 3

Reset to default 8

i am not sure if i have misunderstood your question.

According to my understanding of your question, you need

$('#myModal').modal({
    show: true,
    remote: '/myNestedContent'
});

you cannot just

$('#myModal').modal('show'); 

because there is no url provided in this js method.

does this solve your question?

What you need to do is remove the data-target from the html tag when used with js, leaving it call twice the function then one shows it and the other removes it. Thats how i solved.

Readers should note that the "remote:" option is being deprecated... per the docs

"This option is deprecated since v3.3.0 and will be removed in v4. We remend instead using client-side templating or a data binding framework, or calling jQuery.load yourself.

If a remote URL is provided, content will be loaded one time via jQuery's load method and injected into the .modal-content div. If you're using the data-api, you may alternatively use the href attribute to specify the remote source. An example of this is shown below:"

<a data-toggle="modal" href="remote.html" data-target="#modal">Click me</a>
发布评论

评论列表(0)

  1. 暂无评论