在jQuery自动完成的文本框中添加加载图标

时间:2018-01-23 07:26:26

标签: javascript jquery

我有以下自动填充代码,效果很好。

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

我知道我可以使用addClass和removeClass的jQuery来做。问题是我无法理解我应该在哪一行添加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 );

3 个答案:

答案 0 :(得分:2)

您只需要添加一个类**name1, name2** **DATE** **P1, P2; P1; P2** date1, 2.0, 3.0; 3.4, 2.4 date2, 3.1, 2.3; 2.0, 3.0 ,其背景图片符合指定here

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

.ui-autocomplete-loading

答案 1 :(得分:0)

您可以将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

答案 2 :(得分:0)

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

这是输入密钥的ADD ...以及.post()回调的删除。

$("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);
});