我有一个选择多个列表:
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()事件触发的多重选择的单击选项值
发布评论