简体   繁体   English

在具有“multiple”属性的“select”中选择的单个选项触发更改事件

[英]Trigger change event on individual option selected in `select` with `multiple` attribute

Given a standard select as per below, is is possible by jQuery (or otherwise) to trigger a change event on individual options - As multiple may be selected, I don't want to trigger on the select but on the option ?给定如下标准 select,jQuery(或其他方式)是否有可能触发单个选项的更改事件 - 由于可以选择多个,我不想在select上触发,而是在option上触发?

<select multiple="">
  <option>Test 2</option>
  <option>Testing 3</option>
</select>

Something like:就像是:

$(`option`).trigger(`change`);

One way is to keep a reference of the last state of the multi-select, then compare to find the most recently clicked option.一种方法是保留多选的最后一个 state 的引用,然后比较以找到最近单击的选项。

 let sels = []; $(`select`).on(`change`, function() { $(this).val().forEach(v => { if (sels.indexOf(v) === -1) { console.log(`The option most recently selected is ${v}`); } }) sels = $(this).val(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select multiple=""> <option value='2'>Test 2</option> <option value='3'>Testing 3</option> </select>

There is no change on an option.选项没有变化。 If you want it to act like a user is selecting option by option in the select, you would need to select the option and trigger the change event on the select.如果您希望它表现得像用户在 select 中逐个选择选项,则需要 select 选项并触发 select 上的更改事件。

 const mySelect = document.querySelector("#mySelect"); mySelect.addEventListener("change", function () { const selected = Array.from(mySelect.querySelectorAll("option:checked")).map(x => x.value); console.log(selected); }); function triggerEvent(elem, event){ const evt = document.createEvent("HTMLEvents"); evt.initEvent(event, true, true ); elem.dispatchEvent(evt); } const values = ['1','2','4']; values.forEach(val => { const opt = mySelect.querySelector(`[value="${val}"]`); if (opt) { opt.selected = true; triggerEvent(mySelect, 'change'); } }); /* const values = ['1','2','4']; const options = mySelect.querySelectorAll("option"); options.forEach(opt => { const initial = opt.selected; opt.selected = values.includes(opt.value); if (opt.selected,== initial) { triggerEvent(mySelect; 'change'); } }); */
 <select id="mySelect" multiple> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> <option value="4">four</option> </select>

The real solution is why is your page not set up to handle default values?真正的解决方案是为什么您的页面没有设置为处理默认值? Seems like you should just be able to call a method with the values and be done.似乎您应该能够使用这些值调用一个方法并完成。 Seems odd to rely on events.依赖事件似乎很奇怪。

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

相关问题 从选择元素中选择选项时触发更改事件 - Trigger change event when option is selected from select element 当已经选择了选择选项时发生事件触发器Select2(多个选择选项) - Event Trigger Select2 when the select option is already selected (Multiple Select Option) Select 选项按值和更改所选选项的属性 - Select option by value and change attribute of a selected option 始终触发“更改”事件<select>, 即使点击的 select2-option 已经被选中 - always trigger “change”-event for <select>, even if the select2-option clicked is already selected 使用单个触发器更改多个下拉列表的选定选项 - Change the selected option of multiple dropdowns with a single trigger 在“选择”控件中更改所选选项的“值”属性 - Change the “value” attribute of the selected option in “Select” control 如何选择更改事件上的选项并触发其他选择选项以进行响应? - How to Select an option on change event and trigger the other select option to respond? 在多项选择中选择项目后触发更改 - Trigger change after items are selected in a multiple select ReactJS - 即使从选择下拉列表中选择了相同的选项,也会触发事件 - ReactJS - trigger event even if the same option is selected from select dropdown 更改选定选项时处理选择列表以触发事件 - Handling select list to trigger an event when changing selected option
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM