在我的应用程序中,我想在 ajax 请求完成后手动关闭 Bootstrap 模态视图。
我在回调中调用 jQuery('#myModal').modal('hide');
,
function(){
jQuery('#myModal').modal('hide');
}
但模态视图不会被隐藏。我仔细检查了我在 Bootstrap 之前包含了 jQuery,所以问题不在那里。
我总是收到以下错误:
TypeError: 'undefined' is not a function (evaluating 'jQuery('#myModal').modal('hide')')
模态看起来像这样:
<!-- Modal -->
<div aria-hidden="false" aria-labelledby="modalInviteFlatmateLabel" role="dialog" tabindex="-1" class="modal hide fade in" id="myModal">
<form id="add-flatmate-form" action="/flatmates/show" method="post">
<div class="modal-header">
<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
<h3 id="modalInviteFlatmateLabel">Invite Flatmate</h3>
</div>
<div class="modal-body">
<p>
Enter the mail address of your flatmate you want to invite.
</p><br />
<input placeholder="E-Mail" class="input-block-level" name="AddFlatmateForm[email]" id="AddFlatmateForm_email" type="text" /> </div>
<div class="modal-footer">
<input class="btn btn-primary" type="submit" name="yt0" value="Invite" id="yt0" /> <!--<button class="btn btn-primary">Invite</button>-->
</div>
</form>
</div><!--#myModal-->
附加信息:
- jQuery 版本:1.9.1,
- Bootstrap 版本:2.3.2
有什么想法吗?
最佳答案
来晚了,但我希望这对您有所帮助。 确保在自定义脚本之前包含 jQuery 库,并且 Firebug 的 Net 选项卡中没有错误(确保 jQuery 实际加载)。 在 firebug 中测试您的 javascript 代码,当准备好添加到您的实际代码中时,您可以将其作为匿名函数调用:
(function ($) {
$(document).ready(function(){
//your code
('# id of the modal element to close ').modal('hide')
});
//your code
}(jQuery));
您可以查看此视频 link to tutorial ,我认为他们很好地揭示了调试过程。
关于jQuery Bootstrap : "TypeError: ' undefined' is not a function (evaluating 'jQuery(' #myModal'). 模态 ('hide' )'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18640670/