Index of the item curent being show in a <select> box
I have a select box with the following data
<select>
<option>Bill</option>
<option>Ted</option>
<option>Bogus</option>
</select>
When i select "Bogus" i want to return 2 back to a function, because this is the third item.
Any idea how to do this with jQuery? Something like
jQuery("#colour").change(function(){
alert( $(this).index );
});
开发者_Go百科
doesn't work.
Thanks
After all this jquery business, we forget how it was done before... ;-)
It's
this.selectedIndex
You can also modify your jquery to use the index method
$('option:selected', this).index()
http://jsfiddle.net/rcFGj/
精彩评论