質問編集履歴

1

2020/12/21 03:15

投稿

MSSS.
MSSS.

スコア5

test CHANGED
File without changes
test CHANGED
@@ -17,87 +17,3 @@
17
17
  どこが間違えているのか、あるいは他のやり方はないかを
18
18
 
19
19
  教えていただきたいです。
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
- ```Javascript
28
-
29
- function comment(){
30
-
31
- const submit = document.getElementById("submit");
32
-
33
- if (!submit){ return false;}
34
-
35
- submit.addEventListener("click", (e) => {
36
-
37
- const preschoolId = submit.getAttribute("data-id");
38
-
39
- const formData = new FormData(document.getElementById("comment-form"));
40
-
41
- const XHR = new XMLHttpRequest();
42
-
43
- XHR.open("POST",`/preschools/${preschoolId}/comments`, true);
44
-
45
- XHR.responseType = "json";
46
-
47
- XHR.send(formData);
48
-
49
- XHR.onload = () => {
50
-
51
- if (XHR.status != 200) {
52
-
53
- alert(`Error ${XHR.status}: ${XHR.statusText}`);
54
-
55
- return null;
56
-
57
- }
58
-
59
- const item = XHR.response.comment;
60
-
61
- const itemUser = XHR.response.user;
62
-
63
- if (item.text == "" ) {
64
-
65
- alert(`口コミ欄が空白です`);
66
-
67
- return null;
68
-
69
- }
70
-
71
- const list = document.getElementById("comment-list");
72
-
73
- const formText = document.getElementById("comment-content");
74
-
75
- const HTML = `
76
-
77
- <li class="comments_list">${item.text}
78
-
79
- <a href "/users/${item.user_id}">${itemUser.nickname}さん</a></li>
80
-
81
- `;
82
-
83
- list.insertAdjacentHTML("afterend", HTML);
84
-
85
- formText.value = "";
86
-
87
- };
88
-
89
- e.preventDefault();
90
-
91
- });
92
-
93
- }
94
-
95
-
96
-
97
-
98
-
99
- window.addEventListener("load", comment);
100
-
101
-
102
-
103
- ```