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

php中文网
发布: 2016-06-24 11:48:13
原创
1146人浏览过

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

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

             demo演示

 

立即学习前端免费学习笔记(深入)”;

原理主要是:

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

 

立即学习前端免费学习笔记(深入)”;

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

js部分:

简单听记
简单听记

百度网盘推出的一款AI语音转文字工具

简单听记 269
查看详情 简单听记

<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 infoecho $str;?>
登录后复制

 

立即学习前端免费学习笔记(深入)”;

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号