Why doesn't Firefox show the correct default select option?
I'm making a web app to manage product SKUS. One part of that is to associate SKUs with product names. On each row of a table, I list a SKU and display a <select>
box with product names. The product that's currently associated with that SKU in the database is given an attribute like selected="selected"
. This can be changed and updated via AJAX.
There are a lot of product <option>
s - 103 to be exact - and this list is repeated in the <select>
on each row.
From another input on the page, I am using jQuery AJAX requests to add new SKU/product associations, and to make it clear that they're added instantly, I insert them into the top of the table with a little highlight effect. As the number of SKUs increases past 10 or so, if I refresh the page (which loads everything back out of the database ordered by product name), Firefox starts to show some wrong options as selected by default. It is not consistent about which incorrect option it shows, but it seems to be mixing up the options that existed before the page reload.
If I inspect the <select>
using Firebug, the select="selected"
is on the correct <option>
tag. Refreshing the page (or leaving and typing this page's URL back in to return) does not make it show up correctly, but hard refreshing (Ctrl+F5) does.
Both Chrome and IE7 display this correctly in the first place.
My theory is that this is a result of a faulty cache strategy by Firefox. Does that sound right? Is there any way I can say in my code "if this page is refreshed, make it a hard refresh - reload everything from scratch?"
Update
To solve this problem, I changed strategies.
- Previously, I put a
<select>
with a long list of<option>
s on each table row, with the current value set as default - Now, I put the current value in a
<span>
. If the user clicks a "change" button, I replace the<span>
with a<select>
, and the "change" button becomes a "confirm" button. If they change options and click confirm, AJAX updates the database, the and the<select>
goes back to being a<span>
, this time with the new value.
This has two benefits:
- It fixes the bug described above
- It requires far fewer DOM elements on the page (all those redundant
<option>
s)
I had a similar problem, but after adding autocomplete="off"
HTML attribute to every select tag it worked. [I was using Firefox 8]
Firefox preserves your selected form elements when you refresh. It's intentional. Ctrl+F5 is a "hard" refresh, which disables this behavior.
--
Or Command+Shift+R if you are on a Mac
An easy way to prevent Firefox from caching the last selected option is to remove all of the option elements on page unload. For example (assuming jQuery):
$(window).unload(function() {
$('select option').remove();
});
I had this same issue. I was trying to change the value of the select depending on which option had selected="selected", but Firefox wasn't working. It would always default to the first option.
Chrome, Safari, etc worked when I did this:
$( 'option[value="myVal"]' ).attr( 'selected', 'selected' );
... but this wasn't working in FF.
So I tried:
$( 'option[value="myVal"]' ).prop( 'selected', 'selected' );
and it works.
jQuery v1.9.1
I make it worked by putting the autocomplete="off" on the hidden input.