回答編集履歴
1
調整
answer
CHANGED
@@ -19,4 +19,35 @@
|
|
19
19
|
<input type="checkbox" value="test3店" name="store">test3店<br>
|
20
20
|
<input type="button" class="btn btn-primary" value="選択店舗追加" onclick="storecheck();">
|
21
21
|
<ul id="storeul"></ul>
|
22
|
+
```
|
23
|
+
|
24
|
+
# 削除ボタン
|
25
|
+
```javascript
|
26
|
+
<script>
|
27
|
+
function storecheck() {
|
28
|
+
var store = document.querySelectorAll("[name=store]:checked");
|
29
|
+
var storeul = document.querySelector("#storeul");
|
30
|
+
var storeadded = [...storeul.querySelectorAll('li')].map(x=>[...x.childNodes].filter(x=>x.nodeType==3)[0].textContent);
|
31
|
+
store.forEach ( x=> {
|
32
|
+
const v = x.value;
|
33
|
+
if(!storeadded.includes(v)) {
|
34
|
+
const txt = document.createTextNode(v);
|
35
|
+
const deletebutton = Object.assign(document.createElement('button'),{type:"button",textContent:"削除",className:"del"});
|
36
|
+
const li = [txt,deletebutton].reduce((x,y)=>(x.appendChild(y),x),document.createElement('li'));
|
37
|
+
storeul.appendChild(li);
|
38
|
+
}
|
39
|
+
});
|
40
|
+
if(store.length==0) alert("項目が選択されていません");
|
41
|
+
}
|
42
|
+
document.addEventListener('click',e=>{
|
43
|
+
if(e.target.classList.contains('del')){
|
44
|
+
e.target.closest('li').remove();
|
45
|
+
}
|
46
|
+
});
|
47
|
+
</script>
|
48
|
+
<input type="checkbox" value="test1店" name="store">test1店<br>
|
49
|
+
<input type="checkbox" value="test2店" name="store">test2店<br>
|
50
|
+
<input type="checkbox" value="test3店" name="store">test3店<br>
|
51
|
+
<input type="button" class="btn btn-primary" value="選択店舗追加" onclick="storecheck();">
|
52
|
+
<ul id="storeul"></ul>
|
22
53
|
```
|