开发者

Index of the item curent being show in a <select> box

开发者 https://www.devze.com 2023-03-04 19:36 出处:网络
I have a select box with the following data <select> <option>Bill</option> <option>Ted</option>

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消