简体   繁体   English

如何从 dropdwon 创建所选选项的列表

[英]How do I create a list of the selected options from a dropdwon

I am trying to loop through a multiple select dropdown list and add all of the selected options to a comma separated list.我正在尝试遍历多个 select 下拉列表,并将所有选定的选项添加到逗号分隔的列表中。

My dropdown code is:我的下拉代码是:

<select name="testnameID" id="testnameID" multiple>
  <option value="1">Test number 1</option>
  <option value="2">Test number 2</option>
  <option value="3">Test number 3</option>
  <option value="4">Test number 4</option>
  <option value="5">Test number 5</option>
<select>

In my tag I am using the following, but think it can be simplified or improved:在我的标签中,我使用以下内容,但认为可以简化或改进:

var testnameID = $('#testnameID').val();
var testnameText;            
Array.from(document.querySelector("#testnameID").options).forEach(function(option_element) {
            let option_text = option_element.text;
            let is_option_selected = option_element.selected;

            if (is_option_selected===true){
                testnameText = testnameText + option_text +", ";
                console.log("TestnameText: "+testnameText);
                console.log("\n\r");
            }
            
        });

I need to generate a variable, testnameText, which if the first three items were selected, would return a value of "Test number 1, Test number 2, Test number 3"我需要生成一个变量 testnameText,如果选择了前三个项目,它将返回一个值“测试编号 1,测试编号 2,测试编号 3”

I'm getting myself in a muddle!我把自己弄糊涂了!

You can try using Document.querySelectorAll() to target all the selected options like the following way:您可以尝试使用Document.querySelectorAll()来定位所有选定的选项,如下所示:

 Array.from(document.querySelectorAll("#testnameID option:checked")).forEach(function(option_element) { let option_text = option_element.text; var testnameText = option_text +", "; console.log("TestnameText: "+testnameText); console.log("\n\r"); });
 <select name="testnameID" id="testnameID" multiple> <option value="1" selected>Test number 1</option> <option value="2" selected>Test number 2</option> <option value="3" selected>Test number 3</option> <option value="4">Test number 4</option> <option value="5">Test number 5</option> <select>

You can also try using Array.prototype.map() and Arrow function expressions which is more shorter.您也可以尝试使用更短的Array.prototype.map()Arrow function expressions

The following example creates an array of the selected options:以下示例创建一个包含选定选项的数组:

 var checkedOptions = Array.from(document.querySelectorAll("#testnameID option:checked")); var res = checkedOptions.map(option_element => ("TestnameText: "+option_element.text)); console.log(res);
 <select name="testnameID" id="testnameID" multiple> <option value="1" selected>Test number 1</option> <option value="2" selected>Test number 2</option> <option value="3" selected>Test number 3</option> <option value="4">Test number 4</option> <option value="5">Test number 5</option> <select>

In this case, jquery's each() can help.在这种情况下,jquery 的each()可以提供帮助。 So getting selected options is pretty simple:所以获得选定的选项非常简单:

 $('#testnameID:selected').each(function (index, el) { console.log("TestnameText: " + $(el).text()); });
 <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script> <select name="testnameID" id="testnameID" multiple> <option value="1" selected>Test number 1</option> <option value="2" selected>Test number 2</option> <option value="3" selected>Test number 3</option> <option value="4">Test number 4</option> <option value="5">Test number 5</option> </select>

selectedOptions contains all the selected options of a select element. selectedOptions包含 select 元素的所有选定选项。 You can use it like this:你可以像这样使用它:

 const select = document.querySelector('select'); select.addEventListener('change', e => { // Option texts as an array const texts = Array.from(e.target.selectedOptions).map(({text}) => text); // Option texts as a comma-separated string const textsStr = texts.join(', '); console.log(texts); console.log(textsStr); });
 <select multiple> <option value="1">Test number 1</option> <option value="2">Test number 2</option> <option value="3">Test number 3</option> <option value="4">Test number 4</option> <option value="5">Test number 5</option> </select>

This works outside of the event too, just refer the select element directly instead of e.target .这也适用于事件之外,只需直接引用 select 元素而不是e.target

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从所选项目选项中生成大多数显示值的递增列表? - How do I generate an incrementing list of most appearing values from the selected item options? 如何创建 dropdwon 侧导航栏? - How to create the dropdwon side navbar? 如何从相同ng-options的其他用途中删除选定的ng-options - How do I removed selected ng-options from other uses of same ng-options 如何获得所选选项的数量 - How do I get the number of selected options 当“选项”不是静态时,如何引用“选择”列表的选定值? - How do I make a reference to the selected value of a “select” list when the “options” are not static? 如何基于使用ajax从另一个下拉列表中选择的值来更改下拉列表中的选项? - How do I change the options from a dropdownlist based on the selected value from another dropdownlist using ajax? 从 dropdwon 中选择时,为显示/隐藏文本框提供必填字段 - Give required field for show/hide textbox when selected from the dropdwon 如何从ng-options下拉列表中创建一个“全选”选项,以自动选择所有选项? - How do I create a “select all” option from an ng-options dropdown that automatically selects all of the options? 如何从列表中创建数组 - How do I create an array from a list 如何从下拉列表中选择值? - How do I get selected value from Drop Down List?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM