在多个选择中获取最后点击的选项 [英] Get last clicked option in multiple select

查看:92
本文介绍了在多个选择中获取最后点击的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个页面上,我有一个包含多个选项的选择(多个)框。
现在我想对最后点击的项目做出反应,以ajax显示一些数据。



由于选项元素上的click事件不起作用IE,我目前使用更改事件。



问题是,值和selectedIndex属性指向第一个选定的项目,即使我选择



我可以获得最近选择的选项的唯一方法是通过比较更改事件之前和之后的所选选项集。 / p>

有没有其他的方法?

解决方案


我可以得到最多
最近选择的选项的唯一方法是
比较更改事件之前和之后选定的选项
的集合。


这可能是你最好的选择 - 即不会在单个选项上报告点击事件(它只会在选择框上报告点击)。



如果你真的想弄清楚哪个选项被点击了(当选择框本身听到点击时),你可以看看$ code> offsetY 属性事件对象(这将是鼠标光标相对于选择框中第一个选项顶部的垂直偏移量),因此它包含选择框的滚动偏移量),并将其除以预定的选项大小取决于选择框的字体大小)



但显然,当用户通过键盘选择选项时,这不会帮助您。


On a page, I have a select (multiple) box with many options. Now I want to react on the last clicked item to display some data with ajax.

As the "click" event on the option element does not work in IE, I currently use the "change" event.

The problem is, that the "value" and the selectedIndex attribute point to the first selected item, even if I select other options following the first.

The only way I could get the most recently selected option is by comparing the set of selected options before and after the "change" event.

Is there any other way?

解决方案

The only way I could get the most recently selected option is by comparing the set of selected options before and after the "change" event.

That's probably your best bet -- ie's not going to report click events on the individual options (it will only report clicks on the select box).

If you really want to figure out which option was clicked (when listening for clicks on the select box itself), you can look at the offsetY property of the event object (which will be the vertical offset of the mouse cursor relative to the top of the first option in the select box -- so it includes the select box's scroll offset), and divide that by your pre-determined option size (which will depend on the font-size of the select box).

But obviously, that won't help you when the user selects options via the keyboard.

这篇关于在多个选择中获取最后点击的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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