php实现输入框下拉提示框,输入框自动下拉补全(仿百度、谷歌搜索框提示)

New Document

/*自动下拉补全 zhk */

var highlightindex=-1;//当前高亮的节点

$(document).ready(function(){

var wordInput=$("#word");

var wordInputOffset=wordInput.offset();

$("#auto").hide().css("border","1px black solid").css("position","absolute")

.css("top",wordInputOffset.top+wordInput.height()+5+"px")

.css("left",wordInputOffset.left+"px").width(wordInput.width()+2);

wordInput.keyup(function (event){

var myEvent=event||window.event;

var keyCode=myEvent.keyCode;

if(keyCode>=65&&keyCode<=90||keyCode==8||keyCode==46){

var wordText=$("#word").val();

var autoNode=$("#auto");

if(wordText!=""){

var wordNodes=$("span");

autoNode.html("");

wordNodes.each(function(i){

var wordNode=$(this);

var newDivNode=$("

").attr("id",i);

newDivNode.html(wordNode.text()).appendTo(autoNode);

newDivNode.mouseover(function(){//鼠标进入

if(highlightindex!=-1){

$("#auto").children("div").eq(highlightindex)

.css("background-color","white");

}

highlightindex=$(this).attr("id");

$(this).css("background-color","red");

})

newDivNode.mouseout(function(){//鼠标移除

$(this).css("background-color","white");

})

newDivNode.click(function(){//点击

var comText=$(this).text();

$("#auto").hide();

highlightindex=-1;

$("#word").val(comText);

})

})

if(wordNodes.length>0){

autoNode.show();

}else{

autoNode.hide();

highlightindex=-1;

}

}else{

autoNode.hide();

highlightindex=-1;

}

}else if(keyCode==38||keyCode==40){

if(keyCode==38){//向上

var autoNodes=$("#auto").children("div");

if(highlightindex!=-1){

autoNodes.eq(highlightindex).css("background-color","white");

highlightindex--;

}else{

highlightindex=autoNodes.length-1;

}

if(highlightindex==-1){

highlightindex=autoNodes.length-1;

}

autoNodes.eq(highlightindex).css("background-color","red");

}

if(keyCode==40){

var autoNodes=$("#auto").children("div");

if(highlightindex!=-1){

autoNodes.eq(highlightindex).css("background-color","white");

}

highlightindex++;

if(highlightindex==autoNodes.length){

highlightindex=0;

}

autoNodes.eq(highlightindex).css("background-color","red");

}

}else if(keyCode==13){

if(highlightindex!=-1){

var comText=$("#auto").hide().children("div").eq(highlightindex).text();

highlightindex=-1;

$("#word").val(comText);

}else{

alert("文本框中的【"+$("#word").val()+"】被提交了");

$("#auto").hide();

$("#word").get(0).blur();//失去焦点

}

}

});

$("input [type='button']").click(function(){

alert("文本框中的【"+$("#word").val()+"】被提交了");

});

})

aaa

abc

abd

bbc

beb

cer

erd

beg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值