1jQuery.fn.autocomplete = function(url, settings ) 2{ 3 4 return this.each( function()//do it for each matched element 5 { 6 //this is the original input 7 var textInput = $(this); 8 //create a new hidden input that will be used for holding the return value when posting the form, then swap names with the original input 9 textInput.after('<input type=hidden name="' + textInput.attr("name") + '"/>').attr("name", textInput.attr("name") + "_text"); 10 var valueInput = $(this).next(); 11 //create the ul that will hold the text and values 12 valueInput.after('<ul class="autocomplete"></ul>'); 13 var list = valueInput.next().css({top: textInput.offset().top + textInput.outerHeight(), left: textInput.offset().left, width: textInput.width()}); 14 var oldText = ''; 15 var typingTimeout; 16 var size = 0; 17 var selected = -1; 18 19 settings = jQuery.extend(//provide default settings 20 { 21 minChars : 1, 22 timeout: 1000, 23 after : null, 24 before : null, 25 validSelection : true, 26 parameters : {'inputName' : valueInput.attr('name'), 'inputId' : textInput.attr('id')} 27 } , settings); 28 29 function getData(text) 30 { 31 window.clearInterval(typingTimeout); 32 if (text != oldText && (settings.minChars != null && text.length >= settings.minChars)) 33 { 34 clear(); 35 if (settings.before == "function") 36 { 37 settings.before(textInput,text); 38 } 39 textInput.addClass('autocomplete-loading'); 40 settings.parameters.text = text; 41 // alert('t='+text); 42 $.getJSON(url,settings.parameters,function(data) 43 { 44 var items = ''; 45 if (data) 46 { 47 size = data.length; 48 //alert('s='+size); 49 for (i = 0; i < data.length; i++)//iterate over all options 50 { 51 for ( key in data[i] )//get key => value 52 { 53 items += '<li value="' + key + '">' + data[i][key].replace(new RegExp("(" + text + ")","i"),"<strong>$1</strong>") + '</li>'; 54 } 55 list.html(items); 56 //on mouse hover over elements set selected class and on click set the selected value and close list 57 list.show().children(). 58 hover(function() { $(this).addClass("selected").siblings().removeClass("selected");}, 59 function() { $(this).removeClass("selected") } ). 60 click(function () { 61 // alert('yes'); 62 valueInput.val( $(this).attr('value') ); 63 textInput.val( $(this).text() ); 64 clear(); 65 text1=textInput.val() ; 66 key=$(this).attr('value'); 67 // alert('key='+key+'m='+mainurl); 68 69 document.location='good'+key+'.html'; 70 /* var ur='formir.php'; 71 settings.parameters.word = text1; 72 $.getJSON(ur,settings.parameters,function(data) 73 { 74 // alert('dat='+data); 75 document.location=data;});*/ 76 }); 77 } 78 if (settings.after == "function") 79 { 80 settings.after(textInput,text); 81 } 82 } 83 textInput.removeClass('autocomplete-loading'); 84 }); 85 oldText = text; 86 } 87 } 88 89 function clear() 90 { 91 list.hide(); 92 size = 0; 93 selected = -1; 94 } 95 96 textInput.keydown(function(e) 97 { 98 window.clearInterval(typingTimeout); 99 if(e.which == 27)//escape 100 { 101 clear(); 102 } else if (e.which == 46 || e.which == 8)//delete and backspace 103 { 104 105 clear(); 106 //invalidate previous selection 107 if (settings.validSelection) valueInput.val(''); 108 typingTimeout = window.setTimeout(function() { getData(textInput.val()) },settings.timeout); 109 } 110 else if(e.which == 13)//enter 111 { 112 if ( list.css("display") == "none")//if the list is not visible then make a new request, otherwise hide the list 113 { 114 getData(textInput.val()); 115 } else 116 { 117 text1=textInput.val() ; 118 settings.parameters.word = text1; 119 var ur='formir.php'; 120 $.getJSON(ur,settings.parameters,function(data) 121 { 122 document.location=data; }); 123 clear(); 124 } 125 e.preventDefault(); 126 return false; 127 } 128 else if(e.which == 40 || e.which == 9 || e.which == 38)//move up, down 129 { 130 switch(e.which) 131 { 132 case 40: 133 case 9: 134 selected = selected >= size - 1 ? 0 : selected + 1; break; 135 case 38: 136 selected = selected <= 0 ? size - 1 : selected - 1; break; 137 default: break; 138 } 139 //set selected item and input values 140 textInput.val( list.children().removeClass('selected').eq(selected).addClass('selected').text() ); 141 valueInput.val( list.children().eq(selected).attr('value') ); 142 } else 143 { 144 //invalidate previous selection
145 if (settings.validSelection) valueInput.val(''); 146 typingTimeout = window.setTimeout(function() { getData(textInput.val()) },settings.timeout); 147 } 148 }); 149 }); 150};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.