触发模态

时间:2017-03-26 00:31:02

标签: javascript jquery twitter-bootstrap

我有触发模态的问题, 我检查jquery和它的确定。

我使用的是1.12.4版本,我在代码头中有标记:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 

我的开放式模式标签是:

<a data-toggle="modal" class="modal-trigger" data-id="11" href="#komentarM"></a>

我的模态cocde是:

<div id="komentarM" class="modal">
    <div class="modal-content">
        <h4>Modal Header</h4>
        <p>A bunch of text</p>
    </div>
    <input type="text" name="id" value=""></input>
    <div class="modal-footer">
        <a href="#!" class=" modal-action modal-close waves-effect waves-green btn-flat">Agree</a>
    </div>
</div>

使用此代码,我尝试触发显示模态:

<script> 
$(document).ready(function () {
    $('#komentarM').on('shown.bs.modal', function () {
    alert('da');
    });
});
</script>

只是为了注意,模态是显示但触发不起作用。

3 个答案:

答案 0 :(得分:1)

使用您提供的代码,我在.html文件中创建了此代码:

<!DOCTYPE html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <!-- Latest compiled and minified JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
  integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
      crossorigin="anonymous"></script>
</head>
<body>
  <a data-toggle="modal" class="modal-trigger" data-id="11"
     href="#komentarM">Howdy</a>
</body>

<div id="komentarM" onload="funtiontest()" class="modal">
  <div class="modal-content">
    <h4>Modal Header</h4>
    <p>A bunch of text</p>
  </div>
  <input type="text" name="id" value=""></input>
  <div class="modal-footer">
    <a href="#!" class=" modal-action modal-close waves-effect waves-green btn-flat">Agree</a>
  </div>
</div>

<script>
$(document).ready(function () {
    $('#komentarM').on('shown.bs.modal', function () {
    alert('da');
    });
});
</script>

请注意,我在Howdy标记内添加了<a>,意在触发模态。这使得用户可以点击一个区域。我点击链接,出现警报,然后是模态。这适用于Safari 10.0.3。希望这会有所帮助。

答案 1 :(得分:0)

我认为您的HTML代码中没有错误。正如您在下面看到的,它在Chrome / Firefox中正常运行。我希望这对你有用

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.1/css/materialize.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  </head>
  <body>

  <a data-toggle="modal" class="modal-trigger" data-id="11" href="#komentarM">Modal Open</a>

  <div id="komentarM" class="modal">
      <div class="modal-content">
          <h4>Modal Header</h4>
          <p>A bunch of text</p>
      </div>
      <input type="text" name="id" value=""></input>
      <div class="modal-footer">
          <a href="#!" class=" modal-action modal-close waves-effect waves-green btn-flat">Agree</a>
      </div>
  </div>

  <script type="text/javascript">
    $(document).ready(function(){
      $('#komentarM').on('shown.bs.modal', function () {
        alert('da');
      });
    });
  </script>
  </body>
  </html>

答案 2 :(得分:0)

首先:输入代码不需要关闭,因此我建议您删除input的关闭代码并保留如下:

<input type="text" name="id" value="">

第二个: data-togglehref属性已经链接锚点以打开模态,因此您不需要使用javascript函数来执行此操作,只需使用您的HTML代码

See it working here