查看仅在多选择中选择哪个选项

发布于 2025-01-20 08:37:19 字数 893 浏览 0 评论 0原文

我有一个多选引导选择器,想找到刚刚选择的特定选项。例如,如果我将选择器设置为如下所示:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟柳画桥 2025-01-27 08:37:19

您可以通过以下两种方法使用警报。
第一种方法:使用 OnChange

<select class="form-select selectpicker" multiple aria-label="Default example" onchange = "alert(this.value)">
<option value="Apple">Apple</option>
<option value="Cherry">Cherry</option>
<option value="Papaya">Papaya</option>
<option value="Kiwi">Kiwi</option>
</select>

第二种方法:涉及事件监听器,但它做同样的事情(Javascript)(假设 Select 标签的 ID 是 selector

function change(){
    alert(this.value)
}
document.getElementByID("selector").addEventListener("onchange",change)

Here Are Two Methods By which you can Use Alert.
First Method: With OnChange

<select class="form-select selectpicker" multiple aria-label="Default example" onchange = "alert(this.value)">
<option value="Apple">Apple</option>
<option value="Cherry">Cherry</option>
<option value="Papaya">Papaya</option>
<option value="Kiwi">Kiwi</option>
</select>

Second Method: Involves An Event Listener But Its Doing The Same Thing(Javascript)(Assuming That The ID of The Select Tag is selector

function change(){
    alert(this.value)
}
document.getElementByID("selector").addEventListener("onchange",change)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文