I want to prevent a select box from being changed if a certain condition applies. Doing this doesn't seem to work:
$('#my_select').bind('change', function(ev) {
if(my_condition)
{
ev.preventDefault();
return false;
}
});
I'm guessing this is because by this point the selected option has already changed.
What are other ways of doing this?
Try this:
http://jsfiddle.net/qk2Pc/
You can do this without jquery...
or you can do a function to check your condition
None of the answers worked well for me. The easy solution in my case was:
This accomplishes clicking or tabbing to the select drop down and simply moves the focus to a different field (a nearby text input that was set to readonly) when attempting to focus on the select. May sound like silly trickery, but very effective.
This was the ONLY thing that worked for me (on Chrome Version 54.0.2840.27):
Implement custom readonly like eventHandler
Demo : https://jsfiddle.net/0mvajuay/8/
In the event someone needs a generic version of mattsven's answer (as I did), here it is: