###前提・実現したいこと
タイトルの通りで、一部のクリックイベントが正常に登録されず動作しません。
あるシステムを作っていて、その中で管理者を表に一覧で表示する画面があります。
表の一番右には削除ボタンがあって、それをクリックすると確認画面モーダルウィンドウが表示されます。
さらにそのモーダル内の削除するボタンをクリックすると削除を実行する(ajaxでGET投げる→PHP処理)としたいのですが、
いかんせんイベント自体が動作していないようなので先に進みません。
###発生している問題・該当のソースコード
html
1<!DOCTYPE html> 2<html> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <title>JS Bin</title> 7</head> 8<body> 9 <ol class="breadcrumb"> 10 <li class="active">Home</li> 11 <li class="active">管理者一覧</li> 12 </ol> 13 14 <nav aria-label="..."> 15 <ul class="pager"> 16 <li class="previous"><a onclick="history.back()"><span aria-hidden="true">←</span>戻る</a></li> 17 </ul> 18 </nav> 19 20 <h4 class="page-header">管理者一覧</h4> 21 22 <div style="width:100%;height:300px;overflow:auto;"> 23 <table class="table table-striped table-hover"> 24 <thead> 25 <tr> 26 <th style="width:3%">#</th> 27 <th style="width:10%">ユーザー名</th> 28 <th style="width:15%">名前</th> 29 <th>備考</th> 30 <th style="width:8%">操作</th> 31 </tr> 32 </thead> 33 <tbody> 34 {% for manager in managers %} 35 <tr> 36 <td>{{ manager.id }}</td> 37 <td>{{ manager.user_name }}</td> 38 <td>{{ manager.last_name }} {{ manager.first_name }}</td> 39 <td>{{ manager.remarks }}</td> 40 <td> 41 <button class="btn btn-default btn-sm" value="{{ manager.id }}">編集</button> 42 <button class="btn btn-default btn-sm delete" value="{{ manager.id }}">削除</button> 43 </td> 44 </tr> 45 {% endfor %} 46 </tbody> 47 </table> 48 </div> 49 50 <button class="btn btn-primary button-center">管理者を追加する</button> 51 52 <div id="modalDelete"> 53 <p></p> 54 <button class="btn btn-primary button-center">削除する</button> 55 </div> 56 57<script src="https://code.jquery.com/jquery.min.js"></script> 58<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" /> 59<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 60</body> 61</html>
css
1/* モーダルウィンドウで表示する要素(通常時は非表示) */ 2#modalDelete { 3 display: none; 4} 5/* オーバーレイ要素のスタイル */ 6#modalOverlay { 7 top: 0; 8 left: 0; 9 width: 100%; 10 position: fixed; 11 z-index: 97; 12 background: #000; 13 display: none; 14} 15/* モーダルウィンドウ全体のスタイル */ 16#modalWindow { 17 margin-top: -200px; 18 margin-left: -170px; 19 top: 50%; 20 left: 50%; 21 width: 350px; 22 height: 200px; 23 position: fixed; 24 z-index: 98; 25 background: #fff; 26 display: none; 27} 28/* モーダルウィンドウ内の閉じるボタンのスタイル */ 29#modalWindow > .modalClose { 30 top: -15px; 31 right: -15px; 32 width: 30px; 33 height: 30px; 34 line-height: 30px; 35 color: #fff; 36 font-size: 1.5em; 37 background: #757575; 38 border-radius: 30px; 39 box-shadow: 0 0 3px 0 #000; 40 position: absolute; 41 z-index: 99; 42 cursor: pointer; 43 text-align: center; 44} 45/* コンテンツエリアのスタイル */ 46#modalContents { 47 margin: 30px auto; 48 padding: 0 20px; 49 width: 100%; 50 height: 340px; 51 box-sizing: border-box; 52 overflow-y: auto; 53} 54#modalContents p { 55 padding-bottom: 2em; 56 font-size: 1em; 57 text-align: center; 58 margin-top: 30px; 59}
javascript
1// for manager function 2(function () { 3 // 削除前確認画面モーダルウィンドウの表示処理 4 document.querySelectorAll('.delete').forEach(function (deleteButton) { 5 deleteButton.addEventListener('click', function() { 6 // 取得前にモーダル内削除ボタンに必要な値を設定 7 var modalDeleteButton = document.querySelector('#modalDelete button'); 8 modalDeleteButton.setAttribute('id', 'execDelete'); // 削除実行時のトリガーとしてidをセット 9 // execDelete(); 10 modalDeleteButton.setAttribute('value', deleteButton.getAttribute('value')); // 削除するmanager.id 11 12 var td = deleteButton.parentNode.parentNode.querySelector('td:nth-child(2)'); 13 document.querySelector('#modalDelete p').textContent = td.textContent + ' を削除しますか?'; 14 15 // モーダルウィンドウ内の表示要素を取得 16 var source = document.querySelector('#modalDelete').innerHTML; 17 18 // bodyタグ直前にオーバーレイ要素とモーダルウィンドウ要素を挿入 19 var overlay = document.createElement('div'); 20 overlay.setAttribute('id', 'modalOverlay'); 21 22 var modalWindow = document.createElement('div'); 23 modalWindow.setAttribute('id', 'modalWindow'); 24 modalWindow.innerHTML = 25 '<div class="modalClose">×</div>' 26 + '<div id="modalContents">' 27 + source 28 + '</div>'; 29 30 document.body.appendChild(overlay); 31 document.body.appendChild(modalWindow); 32 33 // 非表示にしていたオーバーレイ要素とモーダルウィンドウ要素をフェードイン表示 34 var modalElements = document.querySelectorAll('#modalOverlay, #modalWindow'); 35 modalElements.forEach(function (element, index) { 36 element.style.display = 'block'; 37 element.style.opacity = 0; 38 39 if (index === 0) { // to overlay 40 element.style.height = window.innerHeight + 'px'; 41 element.style.opacity = 0.7; 42 43 } else if (index === 1) { 44 element.style.opacity = 1; 45 } 46 }); 47 48 // ブラウザウィンドウの高さ変更に合わせてオーバーレイ要素の高さを調整する 49 window.addEventListener('resize', function() { 50 var modalOverlay = document.getElementById('modalOverlay'); 51 if (modalOverlay.style.display === 'block') { 52 modalOverlay.style.height = window.innerHeight; 53 } 54 }, false); 55 56 // モーダル閉じるボタンorオーバーレイをクリック時にモーダルウィンドウを閉じる 57 document.querySelectorAll('#modalOverlay, .modalClose').forEach(function (clickElement) { 58 clickElement.addEventListener('click', function() { 59 document.querySelectorAll('#modalWindow, #modalOverlay').forEach(function (removeElement) { 60 // TODO: add fade out animation... 61 removeElement.parentNode.removeChild(removeElement); 62 }); 63 }, false); 64 }); 65 66 // 削除実行イベント 67 document.getElementById('execDelete').addEventListener('click', function() { 68 console.log('OK'); // <-- モーダル内削除ボタンをクリックしても反応なし。なぜクリックイベントが登録されない?? 69 var ajax = new XMLHttpRequest(); 70 71 ajax.onreadystatechange = function () { 72 if(ajax.readyState === XMLHttpRequest.DONE && ajax.status === 200) { 73 console.log(ajax.responseText); 74 } 75 }; 76 77 ajax.open('GET', '/admin/staff/delete.php?id=' + this.getAttribute('value'), true); 78 ajax.send(null); 79 }, false); 80 }, false); 81 }); 82})();
jsコードの下の方「// 削除実行イベント」とコメントしているところからの
クリックイベント登録コードが該当箇所です。
###テスト環境
テスト環境を以下に用意しました。
https://jsbin.com/cunirez/edit?html,css,js,output
###以上
皆様のお知恵を拝借できれば幸いです。宜しくお願い致します。
回答5件
あなたの回答
tips
プレビュー