jQuery Bootstrap:"TypeError:'undefined'不是一个函数(评估'jQuery('#myModal').modal('hide')')

bur*_*rki 5 html jquery twitter-bootstrap

在我的应用程序中,我想在ajax请求完成后手动关闭引导模式视图.我jQuery('#myModal').modal('hide');在回调中打电话,

function(){                                       
   jQuery('#myModal').modal('hide');
}
Run Code Online (Sandbox Code Playgroud)

但模态视图不会被隐藏.我仔细检查了我在Bootstrap之前包含jQuery,所以问题不在那里.

我总是得到以下错误:

TypeError: 'undefined' is not a function (evaluating 'jQuery('#myModal').modal('hide')')
Run Code Online (Sandbox Code Playgroud)

模态看起来像这样:

   <!-- 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">&times;</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-->
Run Code Online (Sandbox Code Playgroud)

附加信息:

  • jQuery版本:1.9.1,
  • Bootstrap版本:2.3.2

有任何想法吗?

小智 0

已经很晚了,但我希望这会有所帮助。确保在自定义脚本之前包含 jQuery 库,并且 Firebug 的 Net 选项卡中没有错误(确保 jQuery 确实已加载)。在 firebug 中测试您的 javascript 代码,当准备好添加到您的实际代码时,您可以将其称为匿名函数:

(function ($) {
   $(document).ready(function(){
     //your code
     ('# id of the modal element to close ').modal('hide')
   });
//your code
}(jQuery));
Run Code Online (Sandbox Code Playgroud)

您可以查看此教程的视频链接,我认为他们很好地展示了调试过程。