0

我有一个 MVC 2 / jQuery 移动应用程序。

在我的主视图中,我有一个可排序的列表。排序按 url 参数sortorder进行。您可以更改排序的下拉列表位于部分视图中,如下所示:

<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>

然后我有一个 jQuery 来检查下拉菜单是否已更改。在更改时,它会使用新的下拉值添加或更改 urlsort参数并重定向到新的 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);
    });
});

由于在排序顺序之前还有一些其他参数,因此可以使用&添加。

要在新加载页面时从 url 设置正确的值,我有以下 jQuery(函数 gup 从 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?

4

1 回答 1

0

I found the answer on my own. As expected I have to refresh the select (dropdown) with $("#sortSelect").selectmenu("refresh"); like this:

$('#sortSelect').ready(function () { 
    var sortorder = gup('sortorder'); 
    if (sortorder != '') $('#sortSelect').val(unescape(sortorder));
    $("#sortSelect").selectmenu("refresh");
}); 

sometimes, easy problems take some time ;)

于 2012-11-13T08:32:52.397 回答