首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >简单的文本框输入自动提示

简单的文本框输入自动提示

作者头像
书童小二
发布2018-09-03 18:45:47
3.4K0
发布2018-09-03 18:45:47
举报
文章被收录于专栏:前端儿前端儿

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

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

demo演示

原理主要是:

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

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

js部分:

<script type="text/javascript" src="./js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){ 
    $(":button").click(function() {
        /* Act on the event */
        if($(":input").val() != ""){ 
            alert("your name is " + $(":input").val());
        }
    });
    $(":input").bind("keyup",function(){ 
        $(".info").empty();
        if($(this).val() == "")  return;
    //    alert($("#name").val());
        $.ajax({ 
            type: 'get',
            url:    'Automatic_prompt_info.php',
            data: {name: $("#name").val()},
            success: function(data){ 
            //    alert(data);
                        var array = new Array();
                array = data.split(",");
                $(".info").append($("<ul></ul>"));
                for(var i=0;i<array.length-1;i++){
                    $(".info ul").append($("<li>"+array[i]+"</li>"));
                }
                
                $(".info ul").on("click",function(event){    //事件委托
                    $("#name").val($(event.target).text());
                    $(".info").empty();
                })
            }
        });
    });

});
</script>

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

    <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>
</head>

<body>

    <div class="wrap">
    <h3>文本框文本自动提示(如输入fish  jack )</h3>
        <form name="form" method="get" action="">
            <fieldset>
            <label for="search"></label>
            <input type="text" name="name" id="name" placeholder="Input your name">
            <button type="button" id="button">search</button>
            </fieldset>
        </form>
        <div class="info">
        
        </div>
        
    </div>

php数据部分:

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

<?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 info
echo $str;
?>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-03-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档