0

今天执行 autocomplete.js 时发生了太多递归错误。在今天之前,当我执行 autocomplete.js 时,我从来没有在 jquery 中看到过这样的错误。我正在使用 jQuery 1.7.2

$(function(){
    $("#search_text").keyup(function(e){

      var sVal = $(this).val();

      if(e.which == 27) {
        $('#sresult_container').remove();
        return;
      }
      if(e.which != 40 && e.which != 38) {      
        $("#search").removeAttr('disabled');
        $.post('http://localhost/website/index.php/search/ajaxResults',{Search:sVal},function(data){
            if(data != "$$$" && data.length != 0) { 
                var sData = data;
                var flag1 = 0;
                var flag2 = 0;
                var tabindex = -1;
                var aFarray = sData.split('$$$');
                $('#sresult_container').remove();
                var $sresult_container = $('<div id="sresult_container"></div>')
                           .css({'position':'absolute','border':'1px solid','background-color':'white','z-index':'10000000','width':'309px'});
                for(var i=0;i<aFarray.length;i++) {   
                    var a = aFarray[i].split('|||');

                    if(i == 0 && a[0] != "") {
                         flag1 = 1;
                         $pages = $('<div id="pages"></div>');
                         $text1 = $('<p></p>').css({'background-color':'silver','text-align':'center','padding':'3px'}).text("Pages");
                        $pages.append($text1);
                        if(a.length > 5) {
                            a = a.slice(0,5);
                        }
                        for(var j=1;j<a.length+1;j++) {
                             tabindex++;
                            $('<div>/div>').css({'padding':'5px','text-align':'center'}).text(a[j-1]).attr({'tabindex':tabindex,'class':'result'}).appendTo($pages);                    
                                }

                            }
                        if(i == 1 && a[0] != "") {
                             flag2 = 1;
                             $articles = $('<div id="articles"></div>');
                             $text2 = $("<p></p>").css({'background-color':'silver','text-align':'center','padding':'3px'}).text("Articles");
                             $articles.append($text2);

                                if(a.length > 5) {
                                    a = a.slice(0,5);
                                }

                                for(var j=0;j<a.length;j++) {
                                        tabindex++;
                                        $('<div></div>').css({'padding':'5px','text-align':'center'}).text(a[j]).attr({'tabindex':tabindex,'class':'result'}).appendTo($articles);                                  
                                }

                        }

                    }
                    if(flag1 == 0)
                    {
                        $articles.children().first().remove();
                        $div = $sresult_container.append($articles);
                    }else if(flag2 == 0)
                    {
                        $pages.children().first().remove();
                        $div = $sresult_container.append($pages);
                    }else
                    {
                        $div = $sresult_container.append($pages,$articles);
                    }

                    tabindex++;
                 $allresluts = $('<div id="allresults"></div>').css({'padding':'5px','text-align':'center','background-color':'#FBEE92','color':'#CC3333'}).text("See All Results").attr('tabindex',tabindex).appendTo($div);

                    var bottom = $('#search_text').offset();
                    var height = $('#search_text').outerHeight();           
                    var left = bottom.left;
                    var top = bottom.top+height;
                    $div.offset({'top':top,'left':left});


                    $('body').append($div);


                }
                else
                {
                    $('#sresult_container').remove();
                    $("#search").attr('disabled','true');
                }
            });

        }
        else
        {
         $con_div = $('#sresult_container').children().children('div').add($('#sresult_container').children().last());


            var tabindex = $con_div.length - 1;

                    if(e.which == 40)
                    {

                        $con_div.first().addClass("selected").focus();

                        var index = $con_div.first().index(this)+1;
                        $con_div.bind({
                            keydown: function(e) {
                                e.preventDefault();
                                var key = e.keyCode;
                                var target = $(e.target);
                                switch(key) {
                                    case 38: // arrow up
                                        if(index == 0)
                                        {
                                            index = tabindex+1;
                                        }
                                        $con_div[--index].focus();
                                        break;
                                    case 40: // arrow down
                                        if(index > tabindex-1)
                                        {
                                            index = -1;
                                        }
                                        $con_div[++index].focus();
                                        break;
                                    case 13: //Enter 
                                        if(target.hasClass('result') == true)
                                        {
                                            $("#search_text").val(target.text());
                                            $("#search").focus();
                                        }
                                        else
                                        {
                                            $('#search').click();
                                        }
                                        $div.remove();
                                        break;
                                    case 27://Esc
                                        $div.remove();
                                        $("#search_text").focus();  
                                        break;
                                }
                            },

                            focusin: function(e) {
                                $(e.currentTarget).addClass("selected");
                            },

                            focusout: function(e) {
                                $con_div.removeClass("selected");
                                $(e.currentTarget).removeClass("selected");
                            }
                        });




                    }


        }

        setTimeout(function()
        {
        $con_div = $('#sresult_container').children().children('div').add($('#sresult_container').children().last());

        $con_div.live({

                        click : function(e){

                            var $target = $(e.target);
                            if($target.hasClass('result') == true)
                            {
                                $("#search_text").val($target.text());
                                $("#search").focus();
                            }
                            else
                            {
                                $('#search').click();
                            }
                            $('#sresult_container').remove();
                        },
                        mouseover : function(e){

                            var $target = $(e.target);
                            if($target.hasClass('result') == true || $target.is('#allresults'))
                            {
                                $(e.target).css('cursor','pointer');
                                $con_div.removeClass("selected");
                                $(e.target).addClass("selected");
                            }
                        },
                        mouseout : function(){
                            $con_div.removeClass("selected");   
                        }
                });                 
        }, 200 );
    });

    $("#search_text").blur(function(e){
        $con_div = $('#sresult_container').children().children('div').add($('#sresult_container').children().last());

        if($con_div.hasClass('selected') != true)
        {
            $("#sresult_container").remove();   
        }
    });

});

我在 $('#search').click(); 中遇到错误 代码里面。

4

0 回答 0