I have a MVC 2 / jQuery mobile application.
On my main view I have a list that is sortable. The sort works by url parameter sortorder. the dropdown with which you can change the sorting is in a partial view that looks like this:
<div class="ui-select">
<select name="sortSelect" id="sortSelect" data-mini="true">
<option value="opt1">Option 1</option>
<option value="opt2">Option 2</option>
<option value="opt3">Option 3</option>
</select>
</div>
Then I have a jQuery that checks whether the dropdown is changed. On change it adds or changes url sort parameter with new dropdown value and redirects to new url:
$(document).ready(function () {
$('#sortSelect').change(function () {
var sortOrder = $('#sortSelect').val();
var url = document.URL;
if (url.indexOf("sortorder=") > -1)
url = url.replace(/sortorder=[^&]+/, "sortorder=" + sortOrder);
else
url = url + "&sortorder=" + sortOrder;
window.location.replace(url);
});
});
As there are a few other parameters before the sortorder it is ok that is added with &.
To set the correct value from the url when the page is newly loaded I have following jQuery (function gup reads value from url):
$('#sortSelect').ready(function () {
var sortorder = gup('sortorder');
if (sortorder != '')
$('#sortSelect').val(sortorder);
}
There is the problem: if I load the site and change the sortorder by dropdown, everything works so far. The page is newly loaded with new url parameter and newly sorted list. even the dropdown value is set correctly (I checked this by alert($('#sortSelect').val(), but the dropdown text is the old one. If I press F5 the site refreshes, and the dropdown has its correct text…
Any ideas how i can solve the problem? Or what is the problem at all?
I found the answer on my own. As expected I have to refresh the select (dropdown) with
$("#sortSelect").selectmenu("refresh");like this:sometimes, easy problems take some time 😉