I have this simple select:
<select name="zlecenia_index_icpp" id="items_per_page">
<option value="10">10</option>
<option value="25" selected="selected">25</option>
<option value="50">50</option>
</select>
and on it there's:
$('#items_per_page').change(function(){
var controller_action = this.name.replace(/_/g, '/');
location.href = config.base_url + '/' + controller_action + '/'+this.value;
});
It used to work in jQuery 1.3, but in 1.4 the change event is fired as soon as I click on the select box. Is there any solution besides going back to 1.3?
This really seems to be a bug and it has been reported to jQuery:
http://dev.jquery.com/ticket/5869
There has been a patch applied and will be part of jQuery 1.4.1.
http://github.com/jquery/jquery/commit/435772e29b4ac4ccfdefbc4045d43f714e153381
I am still getting a similar issue even with 1.4.1.
Using the example below:
The alert in the third step does not appear in 1.3.2.
I'm not sure if it's supposed to work this way, but you are accessing
this.value
andthis.name
in the same callback. I would assumethis
refers toevent.currentTarget
, which in this case is the SELECT element.In that case,
this.value
will be undefined, but you can try using$(this).val();
instead.Did you try consoling out the variables?
Here's fix for this bug: http://github.com/mcurry/jquery/commit/a293f5938eb9efd41158b948f487672d43b7c820
Hopefully it'll get into 1.4.1
From http://jquery14.com/day-01/jquery-14
OK, this looks like it's a bug, either in IE or JQuery.
What's causing the problem is the selected="selected" attribute on the option is causing the change event to fire before any mouse event occurs. My guess is, it's a weirdness/bug with IE as it appears that it does not set the selected element UNTIL it is visible, thus causing the change even to fire upon the initial dropdown. I say it's a bug in IE because if I call window.event.cancelBubble, the event handler doesn't fire at all.
That's really weird.
The workaround is to remove the selected attribute.
You can use the blur() event of jquery. Perhaps something like this: