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
199 views
in Technique[技术] by (71.8m points)

javascript - Bootstrap $('#myModal').modal('show') is not working

I'm not sure why but all the modal functions are not working with me. I checked the version and the load they are fine.

I keep getting this error message:

Uncaught TypeError: $(...).modal is not a function

for the hide I already found an alternative. instead of:

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

I used this :

$('#myModal .close').click();

And it works perfectly.

The problem now is with the show

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

I also tried both

$('#myModal').modal("toggle");

And:

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

but unfortunately none of them is working.

Here html code -when the button is clicked I will do some verification and handle a json data from the web service if it succeed then I want show my modal- everything is working except the show.

 <button type="button" id="creatNewAcount" class="btn btn-default" data-toggle="modal">Sign up</button>

if there's any alternative I would like to know it.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Most common reason for such problems are

1.If you have defined the jquery library more than once.

<script type="text/javascript" src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

    <div class="modal fade" id="myModal" aria-hidden="true">
    ...
    ...
    </div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

The bootstrap library gets applied to the first jquery library but when you reload the jquery library, the original bootstrap load is lost(Modal function is in bootstrap).

2.Please wrap your JavaScript code inside

$(document).ready(function(){});

3.Please check if the id of the modal is same

as the one you have defined for the component(Also check for duplication's of same id ).


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

...