查看仅在多选择中选择哪个选项
我有一个多选引导选择器,想找到刚刚选择的特定选项。例如,如果我将选择器设置为如下所示:
<select class="form-select selectpicker" multiple aria-label="Default example">
<option value="Apple">Apple</option>
<option value="Cherry">Cherry</option>
<option value="Papaya">Papaya</option>
<option value="Kiwi">Kiwi</option>
</select>
当用户选择第一个选项“Cherry”时,我想发出警报(“Cherry”)。当用户选择第二个选项“Apple”时,我想发出警报(“Apple”)。
我尝试使用 option:selected:last 值,但这仅适用于显示所选选项数组中的最后一个选项。我想显示最近选择的选项,无论它在所选选项数组中的位置如何。任何见解都将受到高度赞赏。
编辑:此问题由于被标记为与此处找到的问题类似而被关闭:使用 JavaScript 在下拉列表中获取选定的值
查看该链接中提供的所有答案后,我不相信问题是相同的。我想获取多选选择器中最近选择的文本,而不是单个选择中唯一选择的选项。因此,如果用户同时选择了 Apple 和 Papaya 选项,但在选择 Papaya 选项后选择了 Apple 选项,我想发出警报(“Apple”)。谢谢。
I have a multiselect bootstrap picker and would like to find the specific option that was just selected. For example, if I have the picker set to something like this:
<select class="form-select selectpicker" multiple aria-label="Default example">
<option value="Apple">Apple</option>
<option value="Cherry">Cherry</option>
<option value="Papaya">Papaya</option>
<option value="Kiwi">Kiwi</option>
</select>
When the user selects their first option, "Cherry", I would like to alert("Cherry"). When the user then selects their second option, "Apple", I would like to alert("Apple").
I have tried to use the option:selected:last value, but this only works to display the last option in the array of selected option. I would like to display the option that was most recently selected, regardless of its place in the array of selected options. Any insight is greatly appreciated.
EDIT: This questions was closed due to being marked as similar to the question found here: Get selected value in dropdown list using JavaScript
After looking through all of the provided answers in that link, I am not convinced that the questions are the same. I would like to get the most recently selected text in a multiselect picker, not the only selected option in a single select. So, if a user has both Apple and Papaya options selected, but selected the Apple option after selecting the Papaya option, I would like to alert("Apple"). Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过以下两种方法使用警报。
第一种方法:使用 OnChange
第二种方法:涉及事件监听器,但它做同样的事情(Javascript)(假设 Select 标签的 ID 是
selector
Here Are Two Methods By which you can Use Alert.
First Method: With OnChange
Second Method: Involves An Event Listener But Its Doing The Same Thing(Javascript)(Assuming That The ID of The Select Tag is
selector