全国统一服务热线:400-633-9193

ajax实现输入提示效果

    网络     2017-12-08    1280

本文实例为大家分享了ajax实现输入提示效果的具体代码,供大家参考,具体内容如下

网站主页

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    *{
      margin:0px auto;
      padding:0px;
    }
    .l{
      height:50px;
      width:198px;
      border-bottom:1px solid black;
      text-align: center;
      line-height:40px;
      vertical-align: middle;
    }
  </style>
  <script src="../wenjian/jquery-2.2.3.min.js"></script>
</head>
<body>
<div style="height: 50px;width: 200px"><input type="text" id="name" style="width: 198px;height: 48px;"></div>
<div id="list" style="height: 500px;width: 200px;border: 1px solid black">
<!--<div id="l">zhongguo</div>-->
</div>
</body>
</html>
<script>
$("#name").keyup(function () {
  var n = $("#name").val();
  if (n != ""){
  $.ajax({
    url:'ltchuli.php',
    data:{n:n},
    type:'post',
//    dataType:'text',
    dataType:'json',
    success:function (data) {
//text写法
//     var s = data.split("|");
//     var str = "";
//     for (var i=0;i<s.length;i++)
//     {
//       str = str + "<div class='l'>" +s[i] +"</div>";
//     }
//     $("#list").html(str);
//json写法
      for (var i in data){
        $("#list").append("<div class='l'>" +data[i] +"</div>");
      }
    }
  });
}else {
    $("#list").html("");
  }
})
</script>

处理页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
/**
 * Created by fcc
 * User: Administrator
 * Date: 2017/10/30
 * Time: 9:52
 */
$n = $_POST['n'];
require_once "../wenjian/DBDA.class.php";
$db = new DBDA();
$obj = "select region_name from region WHERE region_name LIKE '%{$n}%' ";
$data = $db->Query($obj);
//echo $data;
echo json_encode($data);


  分享到:  
0.2498s