更新后“刷新”jquery UI 选择菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10277866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:04:29  来源:igfitidea点击:

'refresh' jquery UI selectmenu after update

jqueryjquery-ui

提问by CLiown

I have an get request which fetches a bunch of category information to populate a <select>. I'm using jQuery UI Selectmenu to style my select.

我有一个 get 请求,它获取一堆类别信息来填充<select>. 我正在使用 jQuery UI Selectmenu 来设计我的选择。

So my jQuery looks a little like this:

所以我的 jQuery 看起来有点像这样:

//Initalise the selectmenu
$("select").selectmenu({ style: 'dropdown' });

$.get("http://localhost/somedata?cat=2", function (data) {
    $.each(data, function (index, itemData) {
        $("<option value='" + itemData.Id + "'>" + itemData.Name + "</option>").appendTo("#selectList");
    });
});

However this populates the <select>but does not update the jQuery UI selectmenu. Any ideas what I need to do to get the selectmenu to be 're-drawn' so the new values appear in the selectmenu?

但是,这会填充<select>但不会更新 jQuery UI 选择菜单。有什么想法可以让选择菜单“重新绘制”以便新值出现在选择菜单中吗?

回答by Frédéric Hamidi

You can use the aptly-named refreshmethod, documented in the development wiki:

您可以使用开发 wiki 中refresh记录的恰当命名的方法:

$("select").selectmenu({ style: 'dropdown' });

$.get("http://localhost/somedata?cat=2", function(data) {
    $.each(data, function(index, itemData) {
        $("<option value='" + itemData.Id + "'>" + itemData.Name
            + "</option>").appendTo("#selectList");
    });

    $("select").selectmenu("refresh");
});

Update:Unfortunately, the refreshfunction is documented but does not seem to be implemented yet. Another option is to destroy the widget and recreate it:

更新:不幸的是,该refresh功能已记录在案,但似乎尚未实现。另一种选择是销毁小部件并重新创建它:

$("select").selectmenu("destroy").selectmenu({ style: "dropdown" });

回答by ababak

That's a very old question so currently there is a much simpler solutionavailable:

这是一个非常古老的问题,所以目前有一个更简单的解决方案

$("select").selectmenu("refresh");

回答by coolguy

Ill recommend the ajax way..try this

我会推荐 ajax 方式..试试这个

$(document).ready(function(){
  $.ajax({
                   url :/somedata?cat=2,          

                   success:function(data){
                        $.each(data, function (index, itemData) {
        $("<option value='" + itemData.Id + "'>" + itemData.Name + "</option>").appendTo("#selectList");
    });

                   }
                });
$("select").selectmenu({ style: 'dropdown' });
});