动态过滤填充input框和用弹出层替换select框

左图是用keydown事件写的动态过滤填充input框

$(document).ready(function(){
	$("input.click").click(function(){$("ul").hide();});
	$("input.click").keydown(function(index){
		position(this);
		$("ul").show();
		$("ul li").each(function(){
			hoverEvent(this);
			$(this).click(function(){
				$("input.click").val("");
				textLi = $(this).text();
				$("ul").hide();
				$("input.click").val(textLi);
			});
		});
	});
});

 

右图是用弹出层替换select框

$(document).ready(function(){
	$("div.click").click(function(index){
		position(this);
		$("ul li:first").addClass("active");
		$("ul li").each(function(){
			if($("div.click span.text").text() == $(this).text()){
				$("ul li").removeClass("active");
				$(this).addClass("active");
			}
			hoverEvent(this);
			$(this).click(function(){
				$("div.click span.text").text("");
				textLi = $(this).text();
				$("ul").hide();
				$("div.click span.text").text(textLi);
			});
		});
	});
});

 

通用的方法

//弹出框定位
function position(obj){
	var leftMargin = $(obj).offset().left;
	var topMargin = $(obj).offset().top;
	leftMarginPX = leftMargin + "px";
	topMarginPX = (topMargin + 25) + "px";
	$("ul").css({left:leftMarginPX,top:topMarginPX});
}

//鼠标hover高亮
function hoverEvent(obj){
	$(obj).hover(
		function(){
			$(obj).addClass("active");
		},
		function(){
			$(obj).removeClass("active");
		}
	);
}

//上下键切换行
var currentLine = -1;
$(document).keydown(function(e){
	if(e.keyCode == 38){
		currentLine--;
		changeItem();	
	}
	else if(e.keyCode == 40){
		currentLine++;
		changeItem();	
	}
});
function changeItem(){
	$("ul li").removeClass("active");
	var lengthNum = $("ul").children("li").length;
	if(currentLine < 0){
		currentLine = lengthNum - 1; 
	}
	if(currentLine == lengthNum){
		currentLine = 0; 
	}
	$("ul li:nth-child("+(currentLine+1)+")").addClass("active");
}  
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值