回答編集履歴
2
修正
test
CHANGED
@@ -132,7 +132,7 @@
|
|
132
132
|
|
133
133
|
function setErrorMessage(selector, string) {
|
134
134
|
|
135
|
-
$(selector).next('.error-message').text(string).sh
|
135
|
+
$(selector).next('.error-message').text(string).removeClass('hide');
|
136
136
|
|
137
137
|
}
|
138
138
|
|
1
追記
test
CHANGED
@@ -13,3 +13,167 @@
|
|
13
13
|
この場合、$('.vali_name')の後ろにerror_message を追加しているだけ。
|
14
14
|
|
15
15
|
すでに存在しているかいないかを判定していないので、何個でも追加されてしまっているのです。
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
---
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
JavaScriptを書きやすくHTMLを作るのも大切です。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
```html
|
28
|
+
|
29
|
+
<!DOCTYPE html>
|
30
|
+
|
31
|
+
<html lang="ja">
|
32
|
+
|
33
|
+
<head>
|
34
|
+
|
35
|
+
<meta charset="UTF-8"/>
|
36
|
+
|
37
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
38
|
+
|
39
|
+
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
<!-- js -->
|
44
|
+
|
45
|
+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
|
46
|
+
|
47
|
+
<script src="./js/post_customer.js" charset="utf-8"></script>
|
48
|
+
|
49
|
+
<style>
|
50
|
+
|
51
|
+
.hide {
|
52
|
+
|
53
|
+
display: none;
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
</style>
|
58
|
+
|
59
|
+
</head>
|
60
|
+
|
61
|
+
<body>
|
62
|
+
|
63
|
+
<header></header>
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
<main id="main" class="post_area">
|
68
|
+
|
69
|
+
<form class="post_form" action="confirm.php" method="post">
|
70
|
+
|
71
|
+
<!-- 名前 -->
|
72
|
+
|
73
|
+
<div class="item">
|
74
|
+
|
75
|
+
<label class="label_block" for="name">
|
76
|
+
|
77
|
+
<i class="fas fa-hand-point-right icon"></i>
|
78
|
+
|
79
|
+
お名前<span class="required">必須</span>
|
80
|
+
|
81
|
+
</label>
|
82
|
+
|
83
|
+
<input id="name"
|
84
|
+
|
85
|
+
class="inputs vali_name"
|
86
|
+
|
87
|
+
type="text"
|
88
|
+
|
89
|
+
name="name"
|
90
|
+
|
91
|
+
autocomplete="off"/>
|
92
|
+
|
93
|
+
<span class="error-message hide">error</span>
|
94
|
+
|
95
|
+
</div>
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
<!-- 送信、リセットボタン -->
|
100
|
+
|
101
|
+
<div class="btn_area">
|
102
|
+
|
103
|
+
<input id="form_btn" type="submit" value="送信する"/>
|
104
|
+
|
105
|
+
<input type="reset" value="リセット"/>
|
106
|
+
|
107
|
+
</div>
|
108
|
+
|
109
|
+
</form>
|
110
|
+
|
111
|
+
</main>
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
<footer></footer>
|
116
|
+
|
117
|
+
</body>
|
118
|
+
|
119
|
+
</html>
|
120
|
+
|
121
|
+
```
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
```javascript
|
126
|
+
|
127
|
+
$(function () {
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
function setErrorMessage(selector, string) {
|
134
|
+
|
135
|
+
$(selector).next('.error-message').text(string).show();
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
$("form.post_form").on('submit', function (e) {
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
let error = 0;
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
let error_message = '必須項目です';
|
150
|
+
|
151
|
+
let error_num = '半角数字のみです';
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
// 名前
|
156
|
+
|
157
|
+
if ($(".vali_name").val() == "") {
|
158
|
+
|
159
|
+
error++;
|
160
|
+
|
161
|
+
setErrorMessage('.vali_name', error_message);
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
if (error) {
|
168
|
+
|
169
|
+
return false;
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
});
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
});
|
178
|
+
|
179
|
+
```
|