模态jQuery不起作用(bootstrap 4)

时间:2017-11-15 11:37:27

标签: jquery twitter-bootstrap bootstrap-modal bootstrap-4

我遇到了jQuery的问题。当我点击链接时,模态打开,我想将data-href的值传递给模态内的按钮。

单击按钮时没有任何反应。请你帮助我好吗?

链接

<a href="#" data-href="stackoverflow.com" data-toggle="modal" data-target="#openModal">Link</a>

模态

<div class="modal fade" id="openModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">         
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                        <button class="btn btn-danger btn-ok" href="">Go</button>
                    </div>
                </div>
            </div>
        </div>

JQuery的

    <script>
        $('#openModal').on('show.bs.modal', function(e) {
            $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
            $('.debug-url').html('Delete URL: <strong>' + $(this).find('.btn-ok').attr('href') + '</strong>');
        });
    </script>

的jQuery /波普/ JS

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.2/umd/popper.min.js"></script> 
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>

错误

Uncaught ReferenceError: $ is not defined

4 个答案:

答案 0 :(得分:1)

尝试使用shown代替show

$('#openModal').on('shown.bs.modal', function(e) {

答案 1 :(得分:0)

试试这个:

$('#openModal').on('show.bs.modal', function(e) {
    var data_href = $(e.relatedTarget).attr('data-href');
    console.log('data_href: ' + data_href);

    $(this).find('.btn-ok').attr('href', data_href);
    $('.debug-url').html('Delete URL: <strong>' + $(this).find('.btn-ok').attr('href') + '</strong>');
});

你做的很好,但你的代码中有一点错误:

$(e.relatedTarget).data('href')

您尝试获取 href 属性,但您要查找的按钮上的属性为 data-href

答案 2 :(得分:0)

我把jQuery文件放在头部而不是身体的底部,它可以工作。谢谢。

答案 3 :(得分:0)

尝试此操作如果您发现此错误&#34;未捕获的ReferenceError:$未定义&#34;而不是取代&#34; $&#34; to&#34; jQuery&#34;。

&#13;
&#13;
<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">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>Some text in the modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
  
</div>

</body>
</html>
&#13;
&#13;
&#13;