After you click the desired data, the data is written to the current input box.
Separate them with commas (,). When the input is continued, the background processes the output data for selection.
Next let's look at the example, html code
The code is as follows: |
Copy code |
<Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/> Ajax Real-time acquisition of drop-down data </pre> <Style> <! -- . Input_s {position: relative} . Input_s ul {list-style: none; margin: 0; padding: 0; width: 200px; border: 1px solid # ccc; border-bottom: none} . Input_s ul li {border-bottom: 1px solid # ccc} . Input_s ul li: hover {color: # fff; background: #000} --> </Style> <Pre> </pre> <Div class = "input_s"> <input class = "tla" id = "tla" style = "width: 500px;" type = "text" name = "tla"/> </Div> <Pre> <Script type = "text/javascript"> // <! [CDATA [ Var funjieliu = function (fn, delay) {// function throttle add by shanmao 2013-1-18 Var timer = null; Return function (){ Var context = this, args = arguments; ClearTimeout (timer ); Timer = setTimeout (function (){ Fn. apply (context, args ); }, Delay ); }; }; Document. getElementById ("tla"). onkeyup = funjieliu (function () {// when the keyboard is pressed Var tla = $ ("# tla"). val (); If (tla ){ $. Post ("/cityosweb/default. php/shanmao/input_xiala", {tla: tla}, function (data ){ If (data. status = 1 ){ $ (". Inul" developer.html (""); $. Each (data. data, function (index, val ){ $ (". Inul"). append (" <Li> "+ val. username +" </li> "); }); } }, "Json "); } },500 ); $ (Function (){ $ (". Inul li"). live ("click", function (){ Var thval = values (this).html (); Var tla = $ ("# tla"). val (); Var regexp = new RegExp (","); If (regexp. test (tla) {// if input has a value (","), add the value in input. $ ("# Tla"). val (tla + thval + ","); } Else { $ ("# Tla"). val (thval + ","); } $ (". Inul" developer.html (""); $ ("# Tla"). focus (); }); }); //]> </Script> |
Php code above
The code is as follows: |
Copy code |
Function input_xiala (){ $ Input = new input (); $ Tval = $ input-> post ('tla '); $ U = M ('user '); If (strpos ($ tval, ",") {// check whether "," $ Val = explode (",", $ tval); // split it into an array $ Tval = end ($ val); // the last value of the array } $ Re = $ u-> field ('username, email ')-> where ("username like' $ tval % '")-> limit (10) -> select (); $ This-> ajaxReturn ($ re, 'success', 1 ); } |