jquery find select options attributes
I know how to get the attributes of an element, using jquery. But I'm not sure how to do it with the actual options in a select field.
<select referenceID="55" name="test" id="test">
<option value="1">first option</option>
<option value="2">second option</option>
<option value="3">third option</option>
</select>
To get the referenceID I would just do this:
$("#test").attr("referenceID");
And when I want to get the value:
$("#test").val();
But I want to get a little more interesting. I want to put some specific开发者_如何学C info into each option:
<select name="test" id="test">
<option value="1" title="something here"*>first option</option>
<option value="2" title="something else here">second option</option>
<option value="3" title="another thing here">third option</option>
</select>
Is it possible to grab an attribute within the options tags?
I intend to have an onselect function that reads the title tag, and does helps me with some other things.
Presuming you want to find the title
attribute of the selected option...
$('#test option:selected').attr('title');
That is, find the descendant element of #test
that is (a) an option element and (b) is selected.
If you already have a selection containing #test
, you can do this with find
:
$('#test').find('option:selected').attr('title');
if you're using onchange, you can use like the following :
$("#test").change(function(){
var title = $(this).find('option:selected').attr('title');
});
Does this not work?
$("#test").change(function() {
var title = $(this).attr("title");
});
精彩评论