Change event on <select>

2019-04-20 14:15发布

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) {
    //??
});

3条回答
爷、活的狠高调
2楼-- · 2019-04-20 14:37

event.target.id is the object

event.target.value is the new value

查看更多
Viruses.
3楼-- · 2019-04-20 14:47

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];
查看更多
相关推荐>>
4楼-- · 2019-04-20 14:49

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];
查看更多
登录 后发表回答