简体   繁体   English

将属性“选定”设置为 select 上的选定选项列出多个

[英]set attribute 'selected' to selected options on select list multiple

I have a list established as multiple, I want that when selecting one or more options to be able to establish the 'selected' attribute to each of the selected options, if any of them is deselected remove the 'selected' attribute from that option, this can be done alone with pure javascript?我有一个建立为多个列表的列表,我希望在选择一个或多个选项时能够为每个选定选项建立“选定”属性,如果取消选择其中任何一个,请从该选项中删除“选定”属性,这可以用纯javascript单独完成吗?

 var selects = document.querySelectorAll('select'); for (const select of selects) { const options = select.options, selected_options = select.selectedOptions, multiple = select.hasAttribute('multiple'); select.addEventListener('change', function(ele) { if (multiple) { // Here should be the event for the multiple list, I can't do it. } else { for (i = 0; i < options.length; i++) { const option = options[i]; Array.prototype.filter.call(option.parentNode.children, function () { if(option.selected) { option.setAttribute('selected', 'selected') } else { option.removeAttribute('selected') } }) } } }) }
 <select name="second" class="form-select" aria-label="Second select example" id="selectSecond"> <option selected>Open this select menu</option> <option value="a">One A</option> <option value="b">Two B</option> <option value="c">Three C</option> </select> <select name="third[]" class="form-select" aria-label="Third select example" id="selectThird" multiple> <option selected>Open this select menu</option> <option value="a1">A One</option> <option value="a2">A Two</option> <option value="a3">A Three</option> <option value="a4">A Four</option> <option value="a5">A Five</option> <option value="a6">A Six</option> </select>

I hope I have explained myself well and you can help me.我希望我已经很好地解释了自己,你可以帮助我。 Thanks in advance.提前致谢。 Kind regards.亲切的问候。

I test it work fine, can you respond to any problems encountered?我测试它工作正常,你能回应任何遇到的问题吗?

 var selects = document.querySelectorAll('select'); for (const select of selects) { const options = select.options, selected_options = select.selectedOptions, multiple = select.hasAttribute('multiple'); select.addEventListener('change', function(ele) { if (multiple) { for (var i = 0; i < options.length; i++) { const option = options[i]; if(option.selected) { option.setAttribute('selected', 'selected') } else { option.removeAttribute('selected') } console.log(option.text,option.getAttribute('selected')); } } else { for (i = 0; i < options.length; i++) { const option = options[i]; Array.prototype.filter.call(option.parentNode.children, function () { if(option.selected) { option.setAttribute('selected', 'selected') } else { option.removeAttribute('selected') } }) } } }) }
 <select name="second" class="form-select" aria-label="Second select example" id="selectSecond"> <option selected>Open this select menu</option> <option value="a">One A</option> <option value="b">Two B</option> <option value="c">Three C</option> </select> <select name="third[]" class="form-select" aria-label="Third select example" id="selectThird" multiple> <option selected>Open this select menu</option> <option value="a1">A One</option> <option value="a2">A Two</option> <option value="a3">A Three</option> <option value="a4">A Four</option> <option value="a5">A Five</option> <option value="a6">A Six</option> </select>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM