001.
<!DOCTYPE html>
002.
<html lang=
"en"
>
003.
<head>
004.
<meta charset=
"UTF-8"
>
005.
<title>Document</title>
007.
<style type=
"text/css"
>
008.
body {
009.
font-family:tahoma,
"Microsoft Sans Serif"
, sans-serif, Verdana;
010.
font-size:12px;
011.
}
012.
#input_q{
013.
width:150px;
014.
}
015.
016.
.ui-autocomplete {
017.
padding-right: 5px;
018.
max-height:200px !important;
019.
overflow: auto !important;
020.
}
021.
022.
.showAll_btn{
023.
position: relative;
024.
top: -2px;
025.
border:0px solid;
026.
font-size: 10px;
027.
height: 23px;
028.
width: 25px;
029.
}
030.
</style>
031.
</head>
032.
<body>
033.
034.
035.
036.
037.
038.
<div style=
"margin:auto;width:80%;"
>
039.
040.
<br><br>
041.
<form id=
"form001"
name=
"form001"
method=
"post"
action=
""
>
042.
<div>Tags:
043.
<input name=
"input_q"
id=
"input_q"
size=
"50"
autofocus>
044.
<button type=
"button"
class
=
"showAll_btn"
>V</button>
045.
<!-- ส่วนสำหรับกำหนดค่า id ของรายการที่เลือก เพื่อไปใช้งาน-->
046.
<input name=
"h_input_q"
type=
"hidden"
id=
"h_input_q"
value=
""
/>
047.
</div>
048.
</form>
049.
050.
<pre>
051.
<?php
052.
053.
if
(
count
(
$_POST
)>0){
054.
print_r(
$_POST
);
055.
}
056.
?>
057.
</pre>
058.
</div>
059.
060.
<script src=
"//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"
></script>
061.
<script src=
"//code.jquery.com/ui/1.11.2/jquery-ui.js"
></script>
062.
063.
<script type=
"text/javascript"
>
064.
$(
function
() {
065.
066.
$(
"#input_q"
).autocomplete({
067.
minLength: 0,
068.
source:
"search.php"
,
069.
open:
function
(){
070.
var
valInput=$(this).val();
071.
if
(valInput!=
""
){
072.
$(
".ui-menu-item a"
).each(
function
(){
073.
var
matcher =
new
RegExp(
"("
+valInput+
")"
,
"ig"
);
074.
var
s=$(this).text();
075.
var
newText=s.replace(matcher,
"<b>$1</b>"
);
076.
$(this).html(newText);
077.
});
078.
}
079.
},
080.
select:
function
( event, ui ) {
081.
082.
console.log( ui.item ?
083.
"Selected: "
+ ui.item.label :
084.
"Nothing selected, input was "
+ this.value);
085.
$(
"#h_input_q"
).val(ui.item.id);
086.
setTimeout(
function
(){
087.
$(
"#h_input_q"
).parents(
"form"
).submit();
088.
},500);
089.
}
090.
});
091.
092.
$(
".showAll_btn"
).click(
function
(){
093.
094.
if
($(
"#input_q"
).autocomplete(
"widget"
).is(
":visible"
) ) {
095.
$(
"#input_q"
).autocomplete(
"close"
);
096.
return
;
097.
}
098.
099.
$(
"#input_q"
).autocomplete(
"search"
,
""
);
100.
101.
$(
"#input_q"
).focus();
102.
});
103.
104.
105.
});
106.
</script>
107.
108.
109.
</body>
110.
</html>