Change event on <select>

荒凉一梦 提交于 2019-12-06 19:18:39

问题


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

回答1:


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];



回答2:


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];



回答3:


event.target.id is the object

event.target.value is the new value



来源:https://stackoverflow.com/questions/2460875/change-event-on-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!