未捕获的TypeError:未定义为Bootstrap Modal

时间:2014-09-15 23:17:08

标签: javascript jquery twitter-bootstrap modal-dialog bootstrap-modal

查看代码:

<div class="modal fade" id="warning_modal">
  ...
</div>

使用Javascript:

if (<%= flash[:warning] %> === true ) {
  console.log("it's now in the function")
  $('#warning_modal').modal({
    show: true,
    keyboard: true
  });
}

我的console.log正在运行且错误位于$('#warning_modal').modal({行,它正在说Uncaught TypeError: undefined is not a function。可能导致这种情况的原因是什么?

注意:我在bootstrap.js文件中需要application.js,如下所示,但这似乎是一个加载问题,因为在控制台中,$('#warning_modal')找到了对象但$('#warning_modal').modal返回undefined

//= require jquery
//= require jquery_ujs
//= require_tree .
//= require bootstrap.js

另外,我尝试通过CDN添加bootstrap.js,但它也没有用。

<head>
  <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js" rel="stylesheet">
</head>

1 个答案:

答案 0 :(得分:1)

你的模态需要id =“warning_modal”,而不是submit_modal

HTML:

<div class="modal fade" id="warning_modal">
...
</div>

代码:

if (<%= flash[:warning] %> === true ) {
console.log("it's now in the function")
$('#warning_modal').modal({
show: true,
keyboard: true
});
}

根据您的更新,请尝试以下订单:

尝试此订单:

//= require jquery
//= require jquery_ujs
//= require bootstrap.min
//= require_tree .