jquery下拉菜单插件,可实现动态数据绑定的下拉框

发布时间:2019-11-14编辑:脚本学堂
jquery下拉菜单插件代码,绑定动态数据的jquery下拉框插件,插件样式文件在jquery插件文件中动态加载,并可以设置jquery插件的的宽、高、默认值等信息。

jquery/xialacaidan/ target=_blank class=infotextkey>jquery下拉菜单插件参数列表:
 

复制代码 代码示例:
//调用方法 $('.xxx').JQSelect({sel_width:"260",sel_height:"28"});
$.fn.selectDefaults = { //默认参数
sel_width:260,//设置select宽度
sel_height:28,//设置select高度
def_val:"-==请选择==-", //设置默认值
sel_val:'{"1":"1111"}',// 数据  格式:{"id":"val"}
item_size:10 //下拉选项显示个数
};


效果图:
 

jquery下拉菜单插件

1、页面代码
 

复制代码 代码示例:
<!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=gb2312" />
<title>绑定动态数据的jquery下拉框插件_www.jb200.com</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="css/js/jquery.jqselect.js"charset="utf-8"></script>
<script>
$(function(){
$("#sel_box").JQSelect();
})
</script>
</head>
<body>
<center>
<span style="font-size:15px; font-weight:bold; text-align:center; line-height:25px; color:#000; width:100%">绑定动态数据的jquery下拉框插件<br />
<a href="http://www.jb200.com" target="_blank" style="color:#000">JquerySchool</a>网站出品(<a href="http://www.jb200.com" style="color:#000" target="_blank">http://www.jb200.com</a>) <br />
<a href="http://www.jb200.com/shop/" target="_blank" style="color:#000">9.9元全国包邮</a> <br /></center>
<div class="sel_box" id="sel_box">       
</div>
</body>
</html>

css代码:
 

复制代码 代码示例:

/* 初始化css */
body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select{margin:0;padding:0}
body{font:12px 'Microsoft YaHei', Arial,'宋体',Helvetica,sans-serif,'Arial Narrow',HELVETICA;background:#fff;-webkit-text-size-adjust:100%;}
a{color:#000;text-decoration:none;display:inline-block;}
a:hover{color:#3468F1;text-decoration:none}
em{font-style:normal}
li{list-style:none}
img{border:0;vertical-align:middle}
table{border-collapse:collapse;border-spacing:0}
p{word-wrap:break-word}

.sel_box{padding:20px; }
.sel_box,.txt_box,.sel_list,.sel_list a{width:260px; height:28px; line-height:28px;display:block;}
.txt_box {border:1px solid #ccc; }
.txt_box .sel_inp{border:0;height:28px; line-height:28px;float:left;width:210px; text-indent:10px;text-align:center;}
.txt_box .sel_span{height:28px;background:#E9E9E9 url(img/sort_down.png) no-repeat 50% 100%;display:inline-block; float:right;width:50px;}
.txt_box .sel_span:hover{background: #CECECE url(img/sort_down_h.png) no-repeat 50% 100%;}
.sel_list{overflow-y:auto; overflow-x:hidden; height:80px;width:260px; border:1px solid #ccc; border-width:0 1px 1px 1px;}
.sel_list a{color:#000; text-decoration:none; background:#fff; text-align:center;width:260px;border-bottom:1px solid #ccc;}
.sel_list a:hover{background:blue;color:#fff;}
.sel_list .last_item{border:0;}

3、jquery.JQSelect.js
 

复制代码 代码示例:

/*
* JQSelect 插件
* Powered By Mr Zhou
* QQ 627266138
* E-mail 627266138qq.com
* Date 2013-10-17
* Dependence jquery-1.7.2.min.js
**/


LoadCSS("css/select.css"); //加载css文件

(function ($) {
//调用方法 $('.xxx').JQSelect({sel_width:"260",sel_height:"28"});
$.fn.selectDefaults = { //默认参数
sel_width:260,//设置select宽度
sel_height:28,//设置select高度
def_val:"-==请选择==-", //设置默认值
sel_val:'{"1":"1111","2":"2222","3":"3333","4":"1111","2":"2222","5":"3333","6":"1111","7":"2222","8":"3333","9":"1111","10":"2222","11":"3333",
"12":"1111","13":"2222","14":"3333"}',// 数据  格式:{"id":"val"}
item_size:10 //下拉选项显示个数
};
$.extendSelect = function (obj,opt) { //obj 元素对象,opt 参数对象
var g = {  //公共方法, 外部可调用
//初始化
init: function () {
var valSize = 0; //获取值个数
var html=""; //初始化select结构
html += "<a href='javascript:void(0);' class='txt_box' id='txt_box'>";
html += "    <input type='text' class='sel_inp' selectable = 'false' id='sel_inp' readonly='readonly' value="+ opt.def_val +" t_valID='0' /><span class='sel_span' id='sel_span'></span>";
html += "</a>";
html += "<div class='sel_list' id='sel_list' style='display:none;'>";
html += "<a href='javascript:void(0);' class='def_val' value='0'>"+ opt.def_val +"</a>";
 var josn_val = $.parseJSON(opt.sel_val); //字符串转json
for(val_item in josn_val) //获取值
{
html += "<a href='javascript:void(0);' a_valID="+ val_item +" >"+ josn_val[val_item] +"</a>";
valSize ++;
}
html += "</div>";
$(g.sel_box).append(html); //将结构导入到列表中对象中
//加入样式
//alert(valSize);
$(g.sel_box).css({width:opt.sel_width});
$("#txt_box",g.sel_box).width(opt.sel_width);
$("#sel_inp",g.sel_box).css({width:(opt.sel_width - $("#sel_span",g.sel_box).width() - 2) + "px", height: opt.sel_height + "px", lineHeight:opt.sel_height + "px"});
$("#sel_span",g.sel_box).css({height:opt.sel_height + "px",lineHeight:opt.sel_height + "px"});
$("#sel_list",g.sel_box).css({height: valSize > opt.item_size ? $("#sel_list a", g.sel_box).height() * opt.item_size +"px " : "auto",width:opt.sel_width});
$("#sel_list a:last",g.sel_box).addClass("last_item");

//下拉点击事件
$("#txt_box",g.sel_box).on('click',function(){
$("#sel_list",g.sel_box).show();
});
//离开隐藏
$(g.sel_box).hover(function(){},function(){$("#sel_list",g.sel_box).hide();});
//值获取
$("#sel_list a",g.sel_box).on('click',function(e){
$("#txt_box #sel_inp",g.sel_box).val($(this).text());
$("#txt_box #sel_inp",g.sel_box).attr("t_valID",$(this).attr('a_valID'));
$("#sel_list",g.sel_box).hide();
});
}
};
g.sel_box = obj; //保存对象
g.init();
return g;
}
$.fn.JQSelect = function (options) {
if (this.length == 0) return; //判断对象是否存在
this.each(function () {
if (this.usedSelect) return;
var opt = $.extend({}, $.fn.selectDefaults, options); //合并已赋值参数
this.usedSelect = $.extendSelect(this, opt);
});
}
})(jQuery);

//导入样式文件
function LoadCSS(url){
var s = document.createElement("LINK");
s.rel = "stylesheet";
s.type = "text/css";
s.href = url;
document.getElementsByTagName("HEAD")[0].appendChild(s);
}

上下箭头,图片如下:
sort_down.pngsort_down_h.png