最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change event on <select> - Stack Overflow

programmeradmin2浏览0评论

With Mootools, if I attach a change event listener on a <select> how do I access the option that was selected. I would like the actual element and not just the value.

$('select').addEvent('change',function(event) {
    //??
});

With Mootools, if I attach a change event listener on a <select> how do I access the option that was selected. I would like the actual element and not just the value.

$('select').addEvent('change',function(event) {
    //??
});
Share Improve this question edited Feb 28, 2020 at 8:06 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Mar 17, 2010 at 9:03 adivasileadivasile 2,4873 gold badges26 silver badges32 bronze badges 2
  • e.target maybe? just guessing here though – knittl Commented Mar 17, 2010 at 9:07
  • event.target only points to the <select> element – adivasile Commented Mar 17, 2010 at 9:13
Add a comment  | 

3 Answers 3

Reset to default 13

Either of these will work:

find by :selected pseudo selector in descendants

this.getElement(':selected');

get first selected value

this.getSelected()[0];

pure javascript, use the selectedIndex property

this.options[this.selectedIndex];

Just access the selectedIndex property on the select element (this object in the event handler) to get the option index.

// get the index of the selected option
var index = this.selectedIndex;

// get the option element
var opt   = this.options[index];

event.target.id is the object

event.target.value is the new value

发布评论

评论列表(0)

  1. 暂无评论