How do you refresh an HTML5 datalist using JavaScript?

前端 未结 7 1069
清歌不尽
清歌不尽 2020-12-10 04:46

I\'m loading options into an HTML5 datalist element dynamically. However, the browser attempts to show the datalist before the options have loaded.

7条回答
  •  隐瞒了意图╮
    2020-12-10 05:28

    Place your #ingredients element is inside #container and try this code:

    $.ajax({
        url: "/api/ingredients",
        data: {search: value.length > 0 ? value + "*" : ""},
        success: function(ingredients) {
            $("#ingredients").remove();
            var item = $('');
            for (var i in ingredients) {                
                item.append("");
            }
            item.appendTo('#container');
        }
    });
    

    even better without #container and using jQuery replaceWith():

    $.ajax({
        url: "/api/ingredients",
        data: {search: value.length > 0 ? value + "*" : ""},
        success: function(ingredients) {
            var item = $('');
            for (var i in ingredients) {                
                item.append("");
            }
            $("#ingredients").replaceWith(item);
        }
    });
    

提交回复
热议问题