简体   繁体   English

从选择框中获取选定的HTML文件

[英]Fetching the selected options html from select box

I have an select dropdown. 我有一个选择下拉列表。 I want to fetch the html of the selected option 我想获取所选选项的html

<select id="filter" multiple="multiple">
     <option>Yes</option>
     <option>No</option>
</select>

For example if I select Yes and No I want the output as 例如,如果我选择是和否,我希望输出为

<option>Yes</option>
<option>No</option>

It can be done with ECMAScript very easily. 使用ECMAScript很容易地做到这一点。

 let select = document.getElementById('filter'); select.addEventListener('change', event => { let checkedOption = [...event.target.children].find(c => c.selected); console.log(checkedOption.outerHTML); }); 
 <select id="filter" multiple="multiple"> <option>Yes</option> <option>No</option> </select> 

 function fun(e){ var concat=''; for(var i = 0; i<e.children.length; i++) { var str = e.children[i].outerHTML concat +=str; } console.log(concat); } 
 <html> <head> <title>JavaScript Popup Example 3</title> </head> <body> <select id="filter" multiple="multiple" onChange='fun(this)'> <option>Yes</option> <option>No</option> </select> </body> </html> 

If you want the output in the console, here you go. 如果要在控制台中输出,请继续。 No jQuery necessary. 无需jQuery。

 filter.addEventListener('change', function() { console.log([...this.querySelectorAll(':checked')]); // even simpler but not available in IE 11: console.log([...filter.selectedOptions]); }) 
 <select id="filter" multiple="multiple"> <option>Yes</option> <option>No</option> </select> 

If what you want is the values from those items which are selected, simply ask the :checked elements for their textContent : 如果您要选择的是这些项目中的值,只需向:checked元素询问其textContent

 filter.addEventListener('change', ()=>{ console.log([...filter.querySelectorAll(':checked')].map(i=>i.textContent)); }) 
 <select id="filter" multiple="multiple"> <option>Yes</option> <option>No</option> </select> 

You can do it using jQuery like this. 您可以使用jQuery这样来实现。

 $("#filter").on('change', function(){ // the next line will find all the options which are selected options = $(this).find('option:selected'); // loop through all the selected options and print them to console for(var i = 0; i < options.length; i++){ option = options[i]; console.log(option.outerHTML); } }); 
 select{ width: 100px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="filter" multiple="multiple"> <option>Yes</option> <option>No</option> </select> 

There was a similar question in Getting the selected values in a multiselect tag in Javascript 在Javascript的multiselect标记中获取选定的值时,存在类似的问题

You can find selected OPTION elements by using element.selected command. 您可以使用element.selected命令找到选定的OPTION元素。 In short, the function for finding these HTML elements would be something like this: 简而言之,用于查找这些HTML元素的函数将如下所示:

var list = document.getElementById("filter");
var button = document.getElementById("button");

button.addEventListener("click", function(e){
    for(var i=0; i<list.length; i++){
    if(list.options[i].selected) {
        console.log(list.options[i]);
    };
  };
});

https://jsfiddle.net/bgz2cr9w/ https://jsfiddle.net/bgz2cr9w/

Using jquery you can get your desired output.


$(document).on('change', '#filter', function(){
    $.each($(this).find('option:selected'), function(i,d){
        console.log(d);
    })
});

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

相关问题 从“选择”框中获取所有选项(选中和未选中) - Get all options from Select box (selected and non selected) 如何限制在 html select 框中选择的选项? - How do you limit options selected in a html select box? 通过 HTML 从 select 框中打印所选信息 - print selected information from select box by HTML 从选择框中删除选项(如果已在其他位置选择) - Remove options from select box if it is already selected in onother 选择其他项目时,在选择框中添加和删除选项 - add and remove options from select box when other item selected 如何使用Javascript Prototype从多个选择框中删除所选选项? - How to remove selected options from multiple select box with Javascript Prototype? 当您单击选择框时,如何通过从api获取值来填充反应选择的选项? - how to populate options for a react select by fetching values from an api to be visible when you click on the select box? HTML select 选项在框中打开 - HTML select options open in box 使用jQuery从另一个选择框的选定值中显示和隐藏一个选择框的选项 - using jQuery show and hide options of one select box from the value selected of another select box 从选择框中动态删除在其他选择框中选择的选项, - Deleting dynamically options from select box that were selected in other select box,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM