如何获取由.change()事件触发的多重选择的单击选项值 [英] How to get clicked option value of a multiple select, fired by .change() event

查看:249
本文介绍了如何获取由.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.

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

See working example on fiddle: http://jsfiddle.net/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()事件触发的多重选择的单击选项值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆