ジャバスクリプトの勉強をしており下記の質問でつまずいてしまったのでどなたかご教授お願いします。
名前とニックネームの追加時、各行の右端に削除ボタンを出力しなさい。
ボタンをクリックするとボタンがある行を削除する処理を作成しなさい。
削除ボタンが一つのセルにしか出ず押して処理する方法がわからない状態です。
<!DOCTYPE html> <html> <head> <title>アカウント登録</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"></html><style> .base { margin: 2% 7% 0; height: 500px; background: blue; padding: 5% 10%; box-shadow: 5px 9px 8px 5px rgba(0, 0, 0, 0.5); } input { width: 300px; height: 40px; font-size: large; } #tuikaBtn { background: green; } h1 { font-family: serif; } .form-control::placeholder { color: #cacaca; opacity: 1; } table { border-collapse: collapse; } th,td { border: 1px solid #312929; padding: 5px 10px; } .card { width: 100%; height: 350px; } .back{ padding: 30px; position: absolute; bottom: 0; right: 0; } .form-group{ margin: 10px 0 15px 0px; } #result { font-size: 120%; margin: 10; margin: 10px 20px; padding: 10px 5px 20px; margin: 10px 5px 10px 5px; } .row .card{ color: red; } .card{ color: blue; display: block; position: relative; } #modal{ display: none; margin:1.5em auto 0; background-color: white; border: 1px solid black; width: 50%; padding: 10px 20px; position: fixed; border-radius: 5px; } #overLay{ display: none; position: fixed; top: 0; left: 0; width: 100%; height: 120%; background-color: rgba(0,0,0,0.75); } </style> </head> <body> <div class="base container"> <div class="row"> <h1>アカウント登録</h1> </div> <div class="row"> <div class="card" id="card"> <div class="card-block"> <form id="form-area" class="form-inline" method="post"> <div class="form-group"> <div class="input-group"> <input id="namae" name="namae" type="text" class="form-control" placeholder="名前"> </div> </div> <div class="form-group"> <div class="input-group"> <input id="nickname" name="nickname" type="text" class="form-control" placeholder="ニックネーム"> </div> </div> <div class="form-group"> <input id="tuikaBtn" type="button" name="touroku" value="追加" onclick="OnButtonClick();"> </div> </form> <div id="tuikaMoto"> </div> <div id="result"> <table id="Tuikatbl"> </table> </div> </div> </div> </div> </div> </div> <div id="overLay"> </div> <script> function OnButtonClick() { if(!targetnamae.value == "" || !targetnickname.value == ""){ var res = confirm(targetnamae.value + "さん" + targetnickname.value + "を登録します。よろしいですか?"); if(res == true){ let newRow = targetTuikatbl.insertRow(); let newCell = newRow.insertCell(); let newText = document.createTextNode(targetnamae.value); newCell.appendChild(newText); newCell = newRow.insertCell(); newText = document.createTextNode(targetnickname.value); newCell.appendChild(newText); newCell = newRow.insertCell(); newCell.appendChild(targetbtn); alert(targetnamae.value + "さん" + targetnickname.value + "にて登録しました"); cntn++; if(cntn == 3){ targetBtn.style.display ="none"; } } } } let cntn = 0; let targetresult = document.getElementById("result") let targetnamae = document.getElementById("namae"); let targetnickname = document.getElementById("nickname"); let targetBtn = document.getElementById("tuikaBtn"); let targetTuikatbl = document.getElementById("Tuikatbl"); let targetbtn = document.createElement("button"); targetbtn.innerHTML = "削除"; </script> </body>
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2023/05/24 23:06