【问题标题】:Add loading icon in the textbox for jQuery Autocomplete在 jQuery 自动完成的文本框中添加加载图标
【发布时间】:2018-01-23 15:43:04
【问题描述】:

我有以下自动完成代码,运行良好。

我只想添加一个类来在 AJAX 调用期间显示加载图像,并在 AJAX 调用完成后删除加载图像。

我知道我可以使用 jQuery 的 addClass 和 removeClass 来做。问题是我无法理解应该在哪一行添加 addClass 以及在哪一行添加 removeClass。

你能帮忙吗?

   (function( $ ) {
        $(function() {
             // Custom autocomplete instance.
            $.widget( "app.autocomplete", $.ui.autocomplete, {

                // Which class get's applied to matched text in the menu items.
                options: {
                    highlightClass: "ui-state-highlight"
                },

                _renderItem: function( ul, item ) {



   // Replace the matched text with a custom span. This
                // span uses the class found in the "highlightClass" option.
                var re = new RegExp( "(" + this.term + ")", "gi" ),
                    cls = this.options.highlightClass,
                    template = "<span class='" + cls + "'>$1</span>",
                    label = item.label.replace( re, template ),
                    $li = $( "<li/>" ).appendTo( ul );

                // Create and return the custom menu item content.
                $( "<a/>" ).attr( "href", "#" )
                           .html( label )
                           .appendTo( $li );

                return $li;

            }

        });
        var autocompleteTextbox = '<div class="autocomplete-search autocomplete-search-open">'
                                    +'<a href="#" class="st-btn01 autocomplete-search-icon"><i class="search-icon" aria-hidden="true"></i></a>'
                                    +'<div class="autocomplete-search-sub1">'
                                        +'<form role="search" method="get" action="<?php echo esc_url( home_url( '+"/"+' ) ); ?>">'
                                            +'<input type="text" name="s" class="form01 autocomplete-search-input" id="autocomplete-search-input" placeholder="Type something" autocomplete="off">'
                                            +'<a href="#" class="st-btn02 autocomplete-search-icon"><i class="searchx-icon" aria-hidden="true"></i></a>'
                                        +'</form>'
                                    +'</div>'
                                +'</div>';

        var appendElement = function(){
            $('.st-navbar-collapse').append(autocompleteTextbox);
            $(this).unbind('click');
            $(".st-search .st-btn01 .search-icon").addClass('text-visibility');
        };
        $(".two-brokers").parent('a').on('click',appendElement);

        var removeElement = function(){
            $(this).parents('.autocomplete-search').remove();
            $(".two-brokers").parent('a').bind('click',appendElement);
            $(".st-search .st-btn01 .search-icon").removeClass('text-visibility');
        };
        $("body").on("click",".st-btn02.autocomplete-search-icon",removeElement);

        var searchRequest,timeoutRequest;
        $("body").on("keyup",".autocomplete-search-input",function(){
            clearTimeout(timeoutRequest);
            var _this = $(this);
            timeoutRequest = setTimeout(function(){
                // create an jq-ui autocomplete on your selected element
                _this.autocomplete({
                    minChars: 2,
                    highlightClass: "bold-text",
                    // use a function for its source, which will return ajax response
                    source: function(request, response){
                        try { searchRequest.abort(); } catch(e){}
                        // well use postautocomplete_search.ajax_url which we enqueued with WP
                        $.post(postautocomplete_search, {
                            action: 'get_test_pages',            // our action is called search
                            term: request.term           // and we get the term form jq-ui
                        }, function(data) {
                            if(!data.length){
                                var result = [{
                                    label: 'No matches found', 
                                    value: response.term
                                }];
                                response(result);
                            }else{
                               response(data);
                            }
                        }, 'json');
                    },
                    select: function( evt, ui ) { 
                        evt.preventDefault();
                        window.location = ui.item.link;
                    }
                });
            },100);
        });
    });
})( jQuery );

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    您只需要添加一个类.ui-autocomplete-loading,其背景图像指定为here

    它会自动检测类,并在请求完成时添加和删除加载图像。

    .ui-autocomplete-loading 
    {
        background: white url("HERE_GOES_LOADING_IMAGE_URL") right center no-repeat;
    }
    

    【讨论】:

      【解决方案2】:

      您可以在模板上将 div 设置为隐藏,然后在需要时进行隐藏和显示

         <div class="loader"></div>
      

      以及相应的 css

      .loader {
          border: 16px solid #f3f3f3; /* Light grey */
          border-top: 16px solid #3498db; /* Blue */
          border-radius: 50%;
          width: 120px;
          height: 120px;
          animation: spin 2s linear infinite;
      }
      
      @keyframes spin {
          0% { transform: rotate(0deg); }
          100% { transform: rotate(360deg); }
      }
      

      所以当你尝试做 ajax 时你应该显示它,当你返回 resule 时隐藏它。

      full example

      【讨论】:

        【解决方案3】:

        我只复制了你的代码的相关部分并添加了我会放 addClass() / removeClass()。

        这是输入键上的 ADD... 和 .post() 回调上的 REMOVE。

        $("body").on("keyup",".autocomplete-search-input",function(){
        
        
          // ADD on keyup
          $("#loaderImgId").addClass("spin");
        
        
          clearTimeout(timeoutRequest);
          var _this = $(this);
          timeoutRequest = setTimeout(function(){
            // create an jq-ui autocomplete on your selected element
            _this.autocomplete({
              minChars: 2,
              highlightClass: "bold-text",
              // use a function for its source, which will return ajax response
              source: function(request, response){
                try { searchRequest.abort(); } catch(e){}
                // well use postautocomplete_search.ajax_url which we enqueued with WP
                $.post(postautocomplete_search, {
                  action: 'get_test_pages',            // our action is called search
                  term: request.term           // and we get the term form jq-ui
                }, function(data) {
        
        
        
                  // REMOVE when Ajax result is in!
                  $("#loaderImgId").removeClass("spin");
        
        
                  if(!data.length){
                    var result = [{
                      label: 'No matches found', 
                      value: response.term
                    }];
                    response(result);
                  }else{
                   response(data);
                  }
                }, 'json');
              },
              select: function( evt, ui ) { 
                  evt.preventDefault();
                  window.location = ui.item.link;
              }
            });
          },100);
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-31
          • 1970-01-01
          • 1970-01-01
          • 2020-04-07
          • 1970-01-01
          • 2013-05-31
          • 2020-07-07
          • 1970-01-01
          相关资源
          最近更新 更多