当前位置:Gxlcms > html代码 > 简单的文本框输入自动提示_html/css_WEB-ITnose

简单的文本框输入自动提示_html/css_WEB-ITnose

时间:2021-07-01 10:21:17 帮助过:159人阅读

简单的文本框输入自动提示--输入的时候可以直接异步加载数据库中匹配的项,然后显示出来。

这里没有使用到数据库,直接在PHP用数组模拟数据存储。

demo演示

原理主要是:

监听输入框的状态,当有改变的时候即刻通过ajax发送数据并取得返回值。

主要使用了jQuery封装很方便,但貌似我这个兼容性不咋地...主要提供个思路吧~

js部分:

顺便把html部分带上,免得不知哪个是哪个

  1. <style type="text/css"> html,body,div,form,input,legend,label,button,ul,li{margin: 0;padding: 0;} form,fieldset{border: 0;} .wrap{position:relative;margin: 100px auto; width: 700px; height: 400px;overflow: hidden;} input{width: 300px; height: 36px; border: 3px solid green;border-radius: 3px;font-weight: bold;} button{width: 120px; height: 42px; border: 0;padding: 8px;margin-left:-10px;background-color: green;font-weight: bold;font-size:16px;color: white;cursor: pointer;border-radius: 30px;} .info{position: relative;top: -10px;left: 14px;width: 305px;} ul{list-style: none;} li{padding: 3px 10px; border-bottom: 1px dotted #333;background-color: #ddd; } li:hover{cursor: pointer;background-color: green;} </style> <div class="wrap"> <h3>文本框文本自动提示(如输入fish jack )</h3> <div class="info"> </div> </div>

php数据部分:

使用简单的正则匹配即可。

  1. <!--?php$names = array('adan','acos','acoss','apple','fish','fisher','fishers','jack','july','boy','boyee','girl','json'); // names$name = $_GET['name']; // name from input label$str = "";$counts = count($names);for($i = 0;$i<$counts;$i++){ if(preg_match("/^$name/", $names[$i])){ //find $str .= $names[$i]; if($i != $counts - 1) $str .= ","; }}//$data = array("A"=-->$str)//echo json_encode($data); // send back infoecho $str;?>

人气教程排行