【问题标题】:Auto open modal by appending variable to selector通过将变量附加到选择器来自动打开模式
【发布时间】:2015-06-26 12:23:48
【问题描述】:

我从引导站点获取了以下代码,如果 int: ModalId 不为 0,则需要自动打开选定的模式。 在 Vis studio 中,.modal 之前的 ')' 下方有一条语法错误红线,而在 chrome 调试器中,它显示“未捕获的类型错误,$(...).modal 不是函数”。请问我做错了什么?

$(document).ready(function() {
       if (@Model.ModalId != 0) {
           $('#accepted_'+@Model.ModalId).modal('show');
       }
    });

【问题讨论】:

    标签: jquery twitter-bootstrap


    【解决方案1】:

    您必须在 index.html 文件中添加 bootstrap.min.js。

    【讨论】:

      【解决方案2】:

      如您所见here,以正确的顺序放置依赖项很重要。

          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
          <script src="//code.jquery.com/jquery-1.10.2.js"></script>
          <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
          <script type="text/javascript">
           $(document).ready(function() {
             if (@Model.ModalId != 0) {
                 $('#accepted_'+@Model.ModalId).modal('show');
             }
            });
          </script>
      

      如果不是这样,请添加更多代码。

      【讨论】:

      • 太好了,谢谢!当我错过了明显的第一步时,我不知道有多少次寻找问题的复杂解决方案。必须停止这样做。由于模态已经开始工作,我认为它不需要任何额外的脚本文件引用,但现在我看到 modal('show') 需要 bootstrap.min.js。
      • 我自己也有类似的问题。当简单的想法导致问题时,我们倾向于搜索不可能的事情。很高兴我提供了帮助。如果你想接受答案:)
      猜你喜欢
      • 2014-03-15
      • 2018-09-01
      • 1970-01-01
      • 2017-08-15
      • 2012-04-02
      • 2011-05-06
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多