質問するログイン新規登録

質問編集履歴

1

コードを追記しました。

2017/11/08 07:43

投稿

okame
okame

スコア54

title CHANGED
File without changes
body CHANGED
@@ -7,11 +7,223 @@
7
7
  いかんせんイベント自体が動作していないようなので先に進みません。
8
8
 
9
9
  ###発生している問題・該当のソースコード
10
+ ```html
11
+ <!DOCTYPE html>
12
+ <html>
13
+ <head>
14
+ <meta charset="utf-8">
15
+ <meta name="viewport" content="width=device-width">
16
+ <title>JS Bin</title>
17
+ </head>
18
+ <body>
19
+ <ol class="breadcrumb">
20
+ <li class="active">Home</li>
21
+ <li class="active">管理者一覧</li>
22
+ </ol>
23
+
24
+ <nav aria-label="...">
25
+ <ul class="pager">
26
+ <li class="previous"><a onclick="history.back()"><span aria-hidden="true">&larr;</span>戻る</a></li>
27
+ </ul>
28
+ </nav>
29
+
30
+ <h4 class="page-header">管理者一覧</h4>
31
+
32
+ <div style="width:100%;height:300px;overflow:auto;">
33
+ <table class="table table-striped table-hover">
34
+ <thead>
35
+ <tr>
36
+ <th style="width:3%">#</th>
37
+ <th style="width:10%">ユーザー名</th>
38
+ <th style="width:15%">名前</th>
39
+ <th>備考</th>
40
+ <th style="width:8%">操作</th>
41
+ </tr>
42
+ </thead>
43
+ <tbody>
44
+ {% for manager in managers %}
45
+ <tr>
46
+ <td>{{ manager.id }}</td>
47
+ <td>{{ manager.user_name }}</td>
48
+ <td>{{ manager.last_name }} {{ manager.first_name }}</td>
49
+ <td>{{ manager.remarks }}</td>
50
+ <td>
51
+ <button class="btn btn-default btn-sm" value="{{ manager.id }}">編集</button>
52
+ <button class="btn btn-default btn-sm delete" value="{{ manager.id }}">削除</button>
53
+ </td>
54
+ </tr>
55
+ {% endfor %}
56
+ </tbody>
57
+ </table>
58
+ </div>
59
+
60
+ <button class="btn btn-primary button-center">管理者を追加する</button>
61
+
62
+ <div id="modalDelete">
63
+ <p></p>
64
+ <button class="btn btn-primary button-center">削除する</button>
65
+ </div>
66
+
67
+ <script src="https://code.jquery.com/jquery.min.js"></script>
68
+ <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
69
+ <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
70
+ </body>
71
+ </html>
72
+ ```
73
+
74
+ ```css
75
+ /* モーダルウィンドウで表示する要素(通常時は非表示) */
76
+ #modalDelete {
77
+ display: none;
78
+ }
79
+ /* オーバーレイ要素のスタイル */
80
+ #modalOverlay {
81
+ top: 0;
82
+ left: 0;
83
+ width: 100%;
84
+ position: fixed;
85
+ z-index: 97;
86
+ background: #000;
87
+ display: none;
88
+ }
89
+ /* モーダルウィンドウ全体のスタイル */
90
+ #modalWindow {
91
+ margin-top: -200px;
92
+ margin-left: -170px;
93
+ top: 50%;
94
+ left: 50%;
95
+ width: 350px;
96
+ height: 200px;
97
+ position: fixed;
98
+ z-index: 98;
99
+ background: #fff;
100
+ display: none;
101
+ }
102
+ /* モーダルウィンドウ内の閉じるボタンのスタイル */
103
+ #modalWindow > .modalClose {
104
+ top: -15px;
105
+ right: -15px;
106
+ width: 30px;
107
+ height: 30px;
108
+ line-height: 30px;
109
+ color: #fff;
110
+ font-size: 1.5em;
111
+ background: #757575;
112
+ border-radius: 30px;
113
+ box-shadow: 0 0 3px 0 #000;
114
+ position: absolute;
115
+ z-index: 99;
116
+ cursor: pointer;
117
+ text-align: center;
118
+ }
119
+ /* コンテンツエリアのスタイル */
120
+ #modalContents {
121
+ margin: 30px auto;
122
+ padding: 0 20px;
123
+ width: 100%;
124
+ height: 340px;
125
+ box-sizing: border-box;
126
+ overflow-y: auto;
127
+ }
128
+ #modalContents p {
129
+ padding-bottom: 2em;
130
+ font-size: 1em;
131
+ text-align: center;
132
+ margin-top: 30px;
133
+ }
134
+ ```
135
+
136
+ ```javascript
137
+ // for manager function
138
+ (function () {
139
+ // 削除前確認画面モーダルウィンドウの表示処理
140
+ document.querySelectorAll('.delete').forEach(function (deleteButton) {
141
+ deleteButton.addEventListener('click', function() {
142
+ // 取得前にモーダル内削除ボタンに必要な値を設定
143
+ var modalDeleteButton = document.querySelector('#modalDelete button');
144
+ modalDeleteButton.setAttribute('id', 'execDelete'); // 削除実行時のトリガーとしてidをセット
145
+ // execDelete();
146
+ modalDeleteButton.setAttribute('value', deleteButton.getAttribute('value')); // 削除するmanager.id
147
+
148
+ var td = deleteButton.parentNode.parentNode.querySelector('td:nth-child(2)');
149
+ document.querySelector('#modalDelete p').textContent = td.textContent + ' を削除しますか?';
150
+
151
+ // モーダルウィンドウ内の表示要素を取得
152
+ var source = document.querySelector('#modalDelete').innerHTML;
153
+
154
+ // bodyタグ直前にオーバーレイ要素とモーダルウィンドウ要素を挿入
155
+ var overlay = document.createElement('div');
156
+ overlay.setAttribute('id', 'modalOverlay');
157
+
158
+ var modalWindow = document.createElement('div');
159
+ modalWindow.setAttribute('id', 'modalWindow');
160
+ modalWindow.innerHTML =
161
+ '<div class="modalClose">×</div>'
162
+ + '<div id="modalContents">'
163
+ + source
164
+ + '</div>';
165
+
166
+ document.body.appendChild(overlay);
167
+ document.body.appendChild(modalWindow);
168
+
169
+ // 非表示にしていたオーバーレイ要素とモーダルウィンドウ要素をフェードイン表示
170
+ var modalElements = document.querySelectorAll('#modalOverlay, #modalWindow');
171
+ modalElements.forEach(function (element, index) {
172
+ element.style.display = 'block';
173
+ element.style.opacity = 0;
174
+
175
+ if (index === 0) { // to overlay
176
+ element.style.height = window.innerHeight + 'px';
177
+ element.style.opacity = 0.7;
178
+
179
+ } else if (index === 1) {
180
+ element.style.opacity = 1;
181
+ }
182
+ });
183
+
184
+ // ブラウザウィンドウの高さ変更に合わせてオーバーレイ要素の高さを調整する
185
+ window.addEventListener('resize', function() {
186
+ var modalOverlay = document.getElementById('modalOverlay');
187
+ if (modalOverlay.style.display === 'block') {
188
+ modalOverlay.style.height = window.innerHeight;
189
+ }
190
+ }, false);
191
+
192
+ // モーダル閉じるボタンorオーバーレイをクリック時にモーダルウィンドウを閉じる
193
+ document.querySelectorAll('#modalOverlay, .modalClose').forEach(function (clickElement) {
194
+ clickElement.addEventListener('click', function() {
195
+ document.querySelectorAll('#modalWindow, #modalOverlay').forEach(function (removeElement) {
196
+ // TODO: add fade out animation...
197
+ removeElement.parentNode.removeChild(removeElement);
198
+ });
199
+ }, false);
200
+ });
201
+
202
+ // 削除実行イベント
203
+ document.getElementById('execDelete').addEventListener('click', function() {
204
+ console.log('OK'); // <-- モーダル内削除ボタンをクリックしても反応なし。なぜクリックイベントが登録されない??
205
+ var ajax = new XMLHttpRequest();
206
+
207
+ ajax.onreadystatechange = function () {
208
+ if(ajax.readyState === XMLHttpRequest.DONE && ajax.status === 200) {
209
+ console.log(ajax.responseText);
210
+ }
211
+ };
212
+
213
+ ajax.open('GET', '/admin/staff/delete.php?id=' + this.getAttribute('value'), true);
214
+ ajax.send(null);
215
+ }, false);
216
+ }, false);
217
+ });
218
+ })();
219
+ ```
220
+
221
+ jsコードの下の方「// 削除実行イベント」とコメントしているところからの
222
+ クリックイベント登録コードが該当箇所です。
223
+
224
+ ###テスト環境
10
225
  テスト環境を以下に用意しました。
11
226
  https://jsbin.com/cunirez/edit?html,css,js,output
12
227
 
13
- jsウィンドウ内のコードの下の方「// 削除実行イベント」とコメントしているところからの
14
- クリックイベント登録コードが該当箇所です。
15
-
16
228
  ###以上
17
229
  皆様のお知恵を拝借できれば幸いです。宜しくお願い致します。