回答編集履歴

3

chousei

2021/06/14 10:50

投稿

yambejp
yambejp

スコア114829

test CHANGED
@@ -44,11 +44,7 @@
44
44
 
45
45
  console.log(txt);
46
46
 
47
- document.querySelectorAll('[type=checkbox]').forEach(x=>{
47
+ document.querySelectorAll('[type=checkbox]').forEach(x=>x.checked=false);
48
-
49
- x.checked=false;
50
-
51
- });
52
48
 
53
49
  localStorage.removeItem('c')
54
50
 

2

ちょうせい

2021/06/14 10:50

投稿

yambejp
yambejp

スコア114829

test CHANGED
@@ -1,12 +1,32 @@
1
1
  formにしてresetすればよいのでは?
2
2
 
3
3
 
4
+
5
+ ※調整版
4
6
 
5
7
  ```javascript
6
8
 
7
9
  <script>
8
10
 
11
+ <script>
12
+
9
13
  window.addEventListener('DOMContentLoaded', ()=>{
14
+
15
+ let c=JSON.parse(localStorage.c||'[]');
16
+
17
+ document.querySelectorAll('[type=checkbox]').forEach((x,y)=>{
18
+
19
+ x.checked=c && c[y];
20
+
21
+ x.addEventListener('change',e=>{
22
+
23
+ c=[...document.querySelectorAll('[type=checkbox]')].map(x=>x.checked);
24
+
25
+ localStorage.c=JSON.stringify(c);
26
+
27
+ });
28
+
29
+ });
10
30
 
11
31
  document.querySelector('#reload').addEventListener('click',()=>{
12
32
 
@@ -20,11 +40,17 @@
20
40
 
21
41
  const t=e.target;
22
42
 
23
- const txt=await fetch('api.php',{method:"post",body:new FormData(t)}).then(res=>res.text());
43
+ const txt=await fetch('y.php',{method:"post",body:new FormData(t)}).then(res=>res.text());
24
-
25
- t.reset();
26
44
 
27
45
  console.log(txt);
46
+
47
+ document.querySelectorAll('[type=checkbox]').forEach(x=>{
48
+
49
+ x.checked=false;
50
+
51
+ });
52
+
53
+ localStorage.removeItem('c')
28
54
 
29
55
  });
30
56
 

1

調整

2021/06/14 10:49

投稿

yambejp
yambejp

スコア114829

test CHANGED
@@ -1 +1,59 @@
1
1
  formにしてresetすればよいのでは?
2
+
3
+
4
+
5
+ ```javascript
6
+
7
+ <script>
8
+
9
+ window.addEventListener('DOMContentLoaded', ()=>{
10
+
11
+ document.querySelector('#reload').addEventListener('click',()=>{
12
+
13
+ location.reload();
14
+
15
+ });
16
+
17
+ document.querySelector('#f1').addEventListener('submit',async (e)=>{
18
+
19
+ e.preventDefault();
20
+
21
+ const t=e.target;
22
+
23
+ const txt=await fetch('api.php',{method:"post",body:new FormData(t)}).then(res=>res.text());
24
+
25
+ t.reset();
26
+
27
+ console.log(txt);
28
+
29
+ });
30
+
31
+ });
32
+
33
+ </script>
34
+
35
+ <form id="f1">
36
+
37
+ <label><input type="checkbox" name="check[]" value="0">0</label><br>
38
+
39
+ <label><input type="checkbox" name="check[]" value="1">1</label><br>
40
+
41
+ <label><input type="checkbox" name="check[]" value="2">2</label><br>
42
+
43
+ <input type="button" value="reload" id="reload">
44
+
45
+ <input type="submit" value="send">
46
+
47
+ </form>
48
+
49
+ ```
50
+
51
+ //api.php
52
+
53
+ ```PHP
54
+
55
+ <?PHP
56
+
57
+ print_r($_POST);
58
+
59
+ ```