Event binding problem on select option (jquery)
I have a select with an option it with id 'option1'. option1 is the currently selected option on page load.
In my $(document).ready, I have:
$(document).ready(function()
{
$("#option1").click(function()
{
alert("Testing");
});
});
However, the alert is never shown and the event never fires! Is it not possible or something to have an event on a select option as opposed to a select itself?
Note:
- I don't think I can bind to the 开发者_如何转开发select's click() event as I only want the event to fire if the current selected option is clicked on / selected again.
- I can't use the select's change() event as I only want it to fire if the currently selected option is clicked on, thus the option won't be changing.
Thanks
1 is correct, at least in IE (it works in Firefox). onclick
will not fire for option
elements.
Binding click on an option is not possible afaik.
Have a look at Click event on select option element in chrome.
Which basically suggest to use the parent select and check if that particular option is selected when fired.
give each option the same class. e.g
<select id='options'><option class='opt' value = '1'>option1/<option class='opt' value = '2'>option2</option></select>
now to listen for an event u can just do this: $('.opt').on('click', function(){ alert($('#options').val());});
精彩评论