簡體   English   中英

單選按鈕將值添加到下拉列表

[英]Radio button adding values to dropdown list

我正在嘗試使用基於選中單選按鈕的值填充下拉列表。 單選按鈕可以添加到列表中,也可以根據選擇的單選從列表中刪除。

我設法為文本字段和提交按鈕編寫代碼,但我需要使用單選按鈕。 非常感謝任何幫助。

 const btnAdd = document.querySelector('#btnAdd'); const btnRemove = document.querySelector('#btnRemove'); const sb = document.querySelector('#list'); const name = document.querySelector('#name'); btnAdd.onclick = (e) => { e.preventDefault(); // validate the option if (name.value == '') { alert('Please enter the name.'); return; } // create a new option const option = new Option(name.value, name.value); // add it to the list sb.add(option, undefined); // reset the value of the input name.value = ''; name.focus(); };
 <div id="container"> <form> <input type="radio" id="studio1" aria-label="Studio 1" value="1" name="studio1">Studio 1 enable<br /> <input type="radio" id="studio1" aria-label="Studio 1" value="1" name="studio1">Studio 1 disable<br /> <input type="radio" id="studio2" aria-label="Studio 2" value="2" name="studio2">Studio 2 enable<br /> <input type="radio" id="studio2" aria-label="Studio 2" value="2" name="studio2">Studio 2 disable<br /> <input type="radio" id="studio3" aria-label="Studio 3" value="3" name="studio3">Studio 3 enable<br /> <input type="radio" id="studio3" aria-label="Studio 3" value="3" name="studio3">Studio 3 disable<br /> <input type="radio" id="studio4" aria-label="Studio 4" value="4" name="studio4">Studio 4 enable<br /> <input type="radio" id="studio4" aria-label="Studio 4" value="4" name="studio4">Studio 4 disable<br /> <label for="name">Studio:</label> <input type="text" id="name" placeholder="Studio Number" autocomplete="off"><br /> <button id="btnAdd">Add</button><br /> <label for="list">Studio List:</label> <select id="list" name="list" multiple></select> </form> </div>

請注意,您在代碼中使用了兩次無效的 HTML - id 必須是唯一的。 將它們更改為類或省略它們,因為它們可能不需要。 你可以這樣做:

 const btnAdd = document.querySelector('#btnAdd'); const btnRemove = document.querySelector('#btnRemove'); const sb = document.querySelector('#list'); const name = document.querySelector('#name'); btnAdd.onclick = (e) => { e.preventDefault(); // validate the option if (name.value == '') { alert('Please enter the name.'); return; } // create a new option const option = new Option(name.value, name.value); // add it to the list sb.add(option, undefined); // reset the value of the input name.value = ''; name.focus(); }; $("input[name^='studio']").on("click", function(e) { let studio = $(this).attr("name"); let studioName = $(this).attr("aria-label"); let studioText = e.currentTarget.nextSibling.data; if (studioText.includes("enable")) { let newoption = $("<option value='" + studio + "'>" + studioName + "</option>"); $("#list").append(newoption); } else { $("#list option[value='" + studio + "']").remove(); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="container"> <form> <input type="radio" class="studio1" aria-label="Studio 1" value="1" name="studio1">Studio 1 enable<br /> <input type="radio" class="studio1" aria-label="Studio 1" value="1" name="studio1">Studio 1 disable<br /> <input type="radio" class="studio2" aria-label="Studio 2" value="2" name="studio2">Studio 2 enable<br /> <input type="radio" class="studio2" aria-label="Studio 2" value="2" name="studio2">Studio 2 disable<br /> <input type="radio" class="studio3" aria-label="Studio 3" value="3" name="studio3">Studio 3 enable<br /> <input type="radio" class="studio3" aria-label="Studio 3" value="3" name="studio3">Studio 3 disable<br /> <input type="radio" class="studio4" aria-label="Studio 4" value="4" name="studio4">Studio 4 enable<br /> <input type="radio" class="studio4" aria-label="Studio 4" value="4" name="studio4">Studio 4 disable<br /> <label for="name">Studio:</label> <input type="text" id="name" placeholder="Studio Number" autocomplete="off"><br /> <button id="btnAdd">Add</button><br /> <label for="list">Studio List:</label> <select id="list" name="list" multiple></select> </form> </div>

請注意,您必須更改單選按鈕的id 否則無法使用 ID。 ID 必須是唯一的。

 const btnAdd = document.querySelector('#btnAdd'); const btnRemove = document.querySelector('#btnRemove'); const sb = document.querySelector('#list'); const name = document.querySelector('#name'); const radios = document.querySelectorAll("#container input[type='radio']"); for(let i = 0; i < radios.length; i++){ radios.item(i).addEventListener("change", function(e){ const studio_num = this.value; const studio_text = "Studio " + studio_num; if(this.getAttribute("id").indexOf("enable") >= 0){ // enable button is clicked // create a new option const option = new Option(studio_text, studio_num); // add it to the list sb.add(option, undefined); } else{ for(let j = 0; j < sb.children.length; j++){ if(sb.children.item(j).value == studio_num){ sb.children.item(j).parentElement.removeChild(sb.children.item(j)); return; } } } }); } btnAdd.onclick = (e) => { e.preventDefault(); // validate the option if (name.value == '') { alert('Please enter the name.'); return; } // create a new option const option = new Option(name.value, name.value); // add it to the list sb.add(option, undefined); // reset the value of the input name.value = ''; name.focus(); };
 <div id="container"> <form> <input type="radio" id="studio1-enable" aria-label="Studio 1" value="1" name="studio1">Studio 1 enable<br /> <input type="radio" id="studio1-disable" aria-label="Studio 1" value="1" name="studio1">Studio 1 disable<br /> <input type="radio" id="studio2-enable" aria-label="Studio 2" value="2" name="studio2">Studio 2 enable<br /> <input type="radio" id="studio2-disable" aria-label="Studio 2" value="2" name="studio2">Studio 2 disable<br /> <input type="radio" id="studio3-enable" aria-label="Studio 3" value="3" name="studio3">Studio 3 enable<br /> <input type="radio" id="studio3-disable" aria-label="Studio 3" value="3" name="studio3">Studio 3 disable<br /> <input type="radio" id="studio4-enable" aria-label="Studio 4" value="4" name="studio4">Studio 4 enable<br /> <input type="radio" id="studio4-disable" aria-label="Studio 4" value="4" name="studio4">Studio 4 disable<br /> <label for="name">Studio:</label> <input type="text" id="name" placeholder="Studio Number" autocomplete="off"><br /> <button id="btnAdd">Add</button><br /> <label for="list">Studio List:</label> <select id="list" name="list" multiple></select> </form> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM