自动完成插件加载图标有时会停止旋转它应该

时间:2015-11-19 22:35:38

标签: jquery jquery-ui jquery-plugins

我正在使用Jquery UI的自动完成插件。我有一个旋转的小图形,只要找到并选择匹配,一切正常。我的问题是,如果没有找到结果或焦点移动,图形仍然可见并旋转。如果没有找到结果,它也会继续旋转,并且永远不会显示未找到的消息。

代码如下,任何帮助将不胜感激。

var _autocomp = {
            source: function(request, response, event) {

                var url = "@Url.Content("~/Jobs/Users")";
                $.getJSON(url, { prefixText: request.term }, function(data) {
                    $.each(data, function(i, Profile) {

                        response($.map(data, function(Profile) {
                            var name = Profile.ID + " - " + Profile.Name;
                            return {
                                label: name,
                                value: Profile.ID
                            };
                        }));
                    });


                });



            },

            minLength: 3,
            delay: 50,
            error:function() {
                $(this).removeClass('loader');
            },
            failure: function() {
                $(this).removeClass('loader');
            },
            select: function(event, ui) {
                this.value = ui.item.label;
                event.preventDefault();
                $(this).removeClass('loader');
            },
            response: function(event, ui) {
                if (ui.content.length === 0) {
                    $(this).text("No results found");
                } else {
                    $(this).empty();
                }

            },
            search: function (e, u) {
                $(this).addClass('loader');
            },
            open:function() {
                $(this).removeClass('loader');
            },
            change: function(event, ui) {
                if (!ui.item) {

                    var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"),
                        valid = false;

                    $('.employeeAutoComp').children("option").each(function () {


                        if ($(this).text().match(matcher)) {
                            this.selected = valid = true;
                            return false;
                        }
                    });
                    if (!valid) { // remove invalid value, as it didn't match anything
                        $(this).val("");
                        $('.employeeAutoComp').val("");
                        //input.data("autocomplete").term = "";
                        return false;
                    }

                }
            }
        };

        $('.employeeAutoComp').autocomplete(_autocomp);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1 个答案:

答案 0 :(得分:0)

尝试删除class&#34; loader&#34;并在complete()方法中显示消息。我发现如果没有结果匹配,则调用完整方法。我有类似的问题,请参阅jQuery UI ui-autocomplete-loading spinner not stopping/disappear when found results