开发者

jQuery - select tag - get attribute of the selected element

开发者 https://www.devze.com 2023-03-04 16:02 出处:网络
Is there a way of getting the attribute - such as \'rel\' from the selected option of the \'select\' tag - i.e.?

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

精彩评论

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