如何获取由.change()事件触发的多重选择的单击选项值

编程入门 行业动态 更新时间:2024-10-28 12:21:49
本文介绍了如何获取由.change()事件触发的多重选择的单击选项值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个选择多个列表:

I have a select multiple list:

<select id="List" multiple="multiple"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> </select>

我在选择项上触发了事件

I trigger the event on the select:

$("select#List").change(function() { // do ajax stuff... });

我需要获取触发更改事件的clicked选项的值.

I need to get the value of the clicked option that fired the change event.

示例: 如果已经选择了选项1,并且我选择了触发.change()事件的选项2",则我想获取选项2"的值.

Example: if the option 1 is already selected, and I select the "option 2" that triggers the .change() event, I want get the value of "option 2".

(我已经阅读了此帖子在多个下拉菜单中获得点击选项,但这对我不起作用.

(I've already read this post Get clicked option in multiple dropdown, but it doesn't work for me).

推荐答案

对您不起作用的是什么,我已经检查了您发布的链接中的答案,并且可以正常工作.

What does not work for you, I've checked the answers in link which you posted and it works.

请参阅提琴上的工作示例: jsfiddle/unkxt8h3/

See working example on fiddle: jsfiddle/unkxt8h3/

这里唯一的事情是,即使您取消选择选项,事件也会被触发.但是检查它是否被选中应该很容易;)

The only thing here is, that the event is fired even when you deselect the option. But checking if it's selected or not should be easy peacy ;)

html:

<select id="List" multiple="multiple"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> </select>

JS:

$("#List option").click(function () { var value = $(this).attr("value"); console.log(value); alert(value); });

更多推荐

如何获取由.change()事件触发的多重选择的单击选项值

本文发布于:2023-10-12 03:04:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483560.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   选项   事件   change

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!