开发者

jQuery - select tag - get attribute of the selected element

Is there a way of getting the attribute - such as 'rel' from the selected option of the 'select' tag - i.e.?

<select name="menu" id="menu">
    <option value="1" rel="123">Option 1</option>
    <option value开发者_如何学Python="2" rel="124">Option 2</option>
</select>

Any idea?


You can just use the :selected filter.

Here's a fiddle : http://jsfiddle.net/jomanlk/ECAea/

$('#menu').change(function(){
    alert($(this).find('option:selected').attr('rel'));
});


with jQuery

$('#menu option:selected').attr('rel');

with javascript

var sel = document.getElementById('menu');
var option = sel.options[sel.selectedIndex];
var rel = option.getAttribute('rel');

demo with both versions at http://jsfiddle.net/gaby/WLFmv/


You can use .attr("attributeName") to get the value of attributes. See .attr().

To find the selected option you can use $('#menu option[selected=true]') or similar.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜