2017-11-15 13 views
0

jQueryに問題があります。リンクをクリックするとモーダルが開き、モーダル内のボタンにdata-hrefの値を渡したいと思います。モーダルjQueryが動作しません(ブートストラップ4)

ボタンをクリックしても何も起こりません。手伝っていただけませんか?

リンク

<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 
+0

他に何も同じIDを持っていないことを確認してください。上記のコードは正常に動作します。ここはjs fiddleにありますhttps://jsfiddle.net/81L3payk/ – devdob

+0

https://jsfiddle.net/81L3payk/1/にチェックしてください –

答えて

0

私は本体の底部の代わりに頭にjQueryのファイルを入れているとできます。ありがとうございました。

1

これを試してみてください代わりにshow

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

0

shownを使用してみてください:あなたが何をしたか

$('#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を取得しようとしていたが、あなたが探しているボタンの属性がデータのhref

0

お試しくださいこのエラーが「Uncaught ReferenceError:$が定義されていません」と表示された場合は、「$」を「jQuery」に置き換えてください。

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

関連する問題