基于jquery实现的可编辑的下拉框

最近做的一个小项目需要用到下拉框,但这个下拉框最好需要可编辑,网上找了几个都不太满意,自己动手改了一个,现在分享给大家。

1、效果如下

225005_N2KQ_2391658.png

2、使用方法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>可编辑的下拉框</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.combox.js"></script>
<link rel="stylesheet" href="styles/style.css" type="text/css"/>
<script type="text/javascript">
$(document).ready(function() {
	$('#combox').combox({datas:['选项一','选项二','选项三']});
})
</script>
</head>
<body>

<span id="combox"></span>
</body>
</html>

3、js源码

(function($){
		$.fn.combox = function(options) {
			var defaults = {  
                borderCss: "combox_border",  
                inputCss: "combox_input",  
                buttonCss: "combox_button",  
                selectCss: "combox_select",
				datas:[]
            };
            var options = $.extend(defaults, options);
			
			function _initBorder($border) {//初始化外框CSS
				$border.css({'display':'inline-block', 'position':'relative'}).addClass(options.borderCss);
				return $border;
			}
			
			function _initInput($border){//初始化输入框
				$border.append('<input type="text" class="'+options.inputCss+'"/>');
				$border.append('<font class="ficomoon icon-angle-bottom '+options.buttonCss+'" style="display:inline-block"></font>');
				//绑定下拉特效
				$border.delegate('font', 'click', function() {
					var $ul = $border.children('ul');
					if($ul.css('display') == 'none') {
						$ul.slideDown('fast');
						$(this).removeClass('icon-angle-bottom').addClass('icon-angle-top');
					}else {
						$ul.slideUp('fast');
						$(this).removeClass('icon-angle-top').addClass('icon-angle-bottom');
					}					
				});
				return $border;//IE6需要返回值
			}
			
			function _initSelect($border) {//初始化下拉列表
				$border.append('<ul style="position:absolute;left:-1px;display:none" class="'+options.selectCss+'">');
				var $ul = $border.children('ul');
				$ul.css('top',$border.height()+1);
				var length = options.datas.length;
				for(var i=0; i<length ;i++)
					$ul.append('<li><a href="javascript:void(0)">'+options.datas[i]+'</a></li>');
				$ul.delegate('li', 'click', function() {
					$border.children(':text').val($(this).text());
					$ul.hide();
					$border.children('font').removeClass('icon-angle-top').addClass('icon-angle-bottom');//确定的时候要将下拉的icon改变
				});
				return $border;
			}
			this.each(function() {
				var _this = $(this);
				_this = _initBorder(_this);//初始化外框CSS
				_this = _initInput(_this);//初始化输入框
				_initSelect(_this);//初始化下拉列表
			});
		};
})(jQuery);

4、css源码

ul{list-style-type:none;margin:0;padding-left:0;}
li{margin:0;}

@font-face {
	font-family: 'icomoon';
	src:url('fonts/icomoon.eot?-fl11l');
	src:url('fonts/icomoon.eot?#iefix-fl11l') format('embedded-opentype'),
		url('fonts/icomoon.woff?-fl11l') format('woff'),
		url('fonts/icomoon.ttf?-fl11l') format('truetype'),
		url('fonts/icomoon.svg?-fl11l#icomoon') format('svg');
	font-weight: normal;
	font-style: normal;
}
.ficomoon{font-family:'icomoon';}
.icon-angle-top:before {content: "\f102"}.icon-angle-bottom:before {content: "\f103"}
/*可根据自己的实际情况做修改*/
.combox_border{border:1px solid #c2c2c2;height:28px;width:245px}
.combox_input{border:0;line-height:25px;height:25px;padding-left: 5px;width:85%;vertical-align: middle;}
.combox_button{width:12%;text-align:center;vertical-align: middle;cursor:pointer;border-left:1px solid #c2c2c2}
.combox_select{border:1px solid #c2c2c2;border-top:0;width:100%}
.combox_select li{overflow:hidden;height:30px;line-height:30px;cursor:pointer;}
.combox_select a {display: block;line-height: 28px;padding: 0 8px;text-decoration: none;color: #666;}
.combox_select a:hover {text-decoration: none;background:#f5f5f5}

5、完整源码下载

http://download.csdn.net/detail/feiyang123_/9578563

 

转载于:https://my.oschina.net/u/2391658/blog/713551

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值