简单的文本框输入自动提示--输入的时候可以直接异步加载数据库中匹配的项,然后显示出来。
这里没有使用到数据库,直接在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 id="文本框文本自动提示-如输入fish-jack">文本框文本自动提示(如输入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标签和属性的使用方法包括:1.基本用法:使用标签如和,通过属性如src和href添加必要信息。2.高级用法:使用data-*自定义属性实现复杂交互。3.避免常见错误:确保属性值用引号包围。4.性能优化:保持简洁,使用标准属性和CSS类名,确保图像有alt属性。掌握这些将提升网页开发技能。

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

HTML的未来将朝着更加语义化、功能化和模块化的方向发展。1)语义化将使标签更明确地描述内容,提升SEO和无障碍访问。2)功能化将引入新元素和属性,满足用户需求。3)模块化将支持组件化开发,提高代码复用性。

htmlattributesarecrucialinwebdevelopment forcontrollingBehavior,外观和功能

alt属性是HTML中标签的重要部分,用于提供图片的替代文本。1.当图片无法加载时,alt属性中的文本会显示,提升用户体验。2.屏幕阅读器使用alt属性帮助视障用户理解图片内容。3.搜索引擎索引alt属性中的文本,提高网页的SEO排名。

HTML、CSS和JavaScript在网页开发中的作用分别是:1.HTML用于构建网页结构;2.CSS用于美化网页外观;3.JavaScript用于实现动态交互。通过标签、样式和脚本,这三者共同构筑了现代网页的核心功能。

设置标签的lang属性是优化网页可访问性和SEO的关键步骤。1)在标签中设置lang属性,如。2)在多语言内容中,为不同语言部分设置lang属性,如。3)使用符合ISO639-1标准的语言代码,如"en"、"fr"、"zh"等。正确设置lang属性可以提高网页的可访问性和搜索引擎排名。

htmlattributeseresene forenhancingwebelements'functionalityandAppearance.TheyAdDinformationTodeFineBehavior,外观和互动,使网站互动,响应式,visalalyAppealing.AttributesLikutesLikeSlikEslikesrc,href,href,href,类,类型,类型,和dissabledtransfransformformformformformformformformformformformformformformforment


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用