回答編集履歴

1

調整

2021/11/18 09:30

投稿

yambejp
yambejp

スコア116835

test CHANGED
@@ -41,3 +41,65 @@
41
41
  <ul id="storeul"></ul>
42
42
 
43
43
  ```
44
+
45
+
46
+
47
+ # 削除ボタン
48
+
49
+ ```javascript
50
+
51
+ <script>
52
+
53
+ function storecheck() {
54
+
55
+ var store = document.querySelectorAll("[name=store]:checked");
56
+
57
+ var storeul = document.querySelector("#storeul");
58
+
59
+ var storeadded = [...storeul.querySelectorAll('li')].map(x=>[...x.childNodes].filter(x=>x.nodeType==3)[0].textContent);
60
+
61
+ store.forEach ( x=> {
62
+
63
+ const v = x.value;
64
+
65
+ if(!storeadded.includes(v)) {
66
+
67
+ const txt = document.createTextNode(v);
68
+
69
+ const deletebutton = Object.assign(document.createElement('button'),{type:"button",textContent:"削除",className:"del"});
70
+
71
+ const li = [txt,deletebutton].reduce((x,y)=>(x.appendChild(y),x),document.createElement('li'));
72
+
73
+ storeul.appendChild(li);
74
+
75
+ }
76
+
77
+ });
78
+
79
+ if(store.length==0) alert("項目が選択されていません");
80
+
81
+ }
82
+
83
+ document.addEventListener('click',e=>{
84
+
85
+ if(e.target.classList.contains('del')){
86
+
87
+ e.target.closest('li').remove();
88
+
89
+ }
90
+
91
+ });
92
+
93
+ </script>
94
+
95
+ <input type="checkbox" value="test1店" name="store">test1店<br>
96
+
97
+ <input type="checkbox" value="test2店" name="store">test2店<br>
98
+
99
+ <input type="checkbox" value="test3店" name="store">test3店<br>
100
+
101
+ <input type="button" class="btn btn-primary" value="選択店舗追加" onclick="storecheck();">
102
+
103
+ <ul id="storeul"></ul>
104
+
105
+ ```