How do I do this SELECT/OPTION in HTML? (javascript)
Suppose I have this:
<select id="myselect">
<option rel="a">1</option>
<option rel="b">2</option>
开发者_开发问答<select>
How do I use JQuery so that onchange $("#myselect")
, I alert the "rel" part of the option (not 1 or 2)
$("#myselect").change(function() {
alert($(this).find("option:selected").attr("rel"));
});
How about this?
$("myselect").change(function(){
$(this).children("option:selected").each(function () {
alert($(this).attr('rel');
});
});
Should work for multiple selects, too.
$("#myselect").change(function() {
$("#myselect option:selected").each(function () {
alert($(this).attr('rel'));
});
});
Just to mention, my code will also work if you have the multiple="multiple"
attribute on your SELECT
element, alerting both values.
The 'rel' attribute is not supported on an option element, you should be using the 'value' attribute to specify a value for your options. I'm sure jQuery can fetch that much easier than an unsupported attribute.
精彩评论