jQuery Bootstrap : "TypeError: ' undefined' is not a function (evaluating 'jQuery(' #myModal'). 模态 ('hide' )')

标签 jquery html twitter-bootstrap

在我的应用程序中,我想在 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">&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-->

附加信息:

  • 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/

相关文章:

javascript - 拆分按钮的按钮部分的用途

css - 在 div 中安装 iframe

html - 在 Chrome 中居中谷歌仪表盘

html - 如何切换两个不同的图像?

javascript - 如何在 javascript onclick 函数中使用 *this* 并使用 jquery 进行遍历

javascript - Force.drag().on 返回未定义

javascript - jQuery datepicker 日期范围抛出无效日期范围错误和有效日期

javascript - 在一页中实现多个带有自定义按钮的TinyMCE编辑器

javascript - 将 href 属性添加到 Bootstrap 按钮

javascript - 使用 Chosen 插件更改选择中的选择