`

jquey写的select下拉插件 解决了自带option宽度不能改变的问题

阅读更多


1,html代码
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
    <script type="text/javascript" src="js/selectAuto.js"></script>
    <link type="text/css" href="css/selectAuto.css" rel="stylesheet"/>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#selectId").selectAuto({
                title:"所有分类",
                width:200,
                height:200,
                data: [
                    {key: "0", value: "所有分类"},
                    {key: "1", value: "测试1"},
                    {key: "2", value: "测试2"},
                    {key: "3", value: "测试3"},
                    {key: "4", value: "测试4"}
                ],
                callback:function(){
                },
                onSelect:function(key,val){
                    alert(key);
                }
            });
        });
    </script>
</head>
<body>
        <div  id="selectId"></div>
</body>
</html>

2,js代码
(function ($) {
    $.fn.selectAuto = function(_4b,_4c){
        if(typeof _4b=="string"){
            var _4d=$.fn.selectAuto.methods[_4b];
            if(_4d){
                return _4d(this,_4c);
            }
        }
        _4b=_4b||{};
        var defaults = {
            title:"",//下拉提示
            width: 200,
            height: 150,
            url:null,    //远程接口加载数据
            type:"get", //接口调用方式 默认是get方式
            dataType:"json",//数据返回格式 默认为json格式
            param:null,
            data:null,    //加载本地数据
            callback:null, //自定义回调方法
            locationStyle:{left: "3px", top:"26px"},//默认位置
            onSelect:function(){}  //下拉选中事件
        }
        var options = $.extend(defaults, _4b);
        this.each(function(){
            var thisSelect=$(this);
            var _ids=thisSelect[0].id;
            thisSelect[0].style.position="relative";
            var html='<span id="'+_ids+'_select_title_span"  class="select_title_span" style="width:'+options.width+'px;"> ' +
                        ' <span id="'+_ids+'_select_title" class="select_title" style="width:'+options.width+'px;">'+options.title+'</span>'+
                        ' <b class="select_icon"></b>'+
                      '</span>'+
                      '<div id="'+_ids+'_option_box_div" class="option_box_div" style="display:none;width:'+(options.width+10)+'px;">'+
                       '  <span id="'+_ids+'_data_li" style="width:'+(options.width+20)+'px;height:'+options.height+'px;">'+
                        ' </span></div>';
            thisSelect.empty().append(html);
            $("#"+_ids+"_option_box_div").css(options.locationStyle);
            if(options.data!=null){
                $.fn.selectAuto.methods. _5d(_ids,options.data);
            }else{
                if(options.url){
                    $.ajax({type:options.type,url:options.url,data:options.param,dataType:options.dataType,success:function(_d){
                        $.fn.selectAuto.methods. _5d(_ids,_d);
                    },error:function(){
                    }});
                }
            }
            if(typeof options.callback=="function"){
                options.callback();
            }
            thisSelect.find("span#"+_ids+"_select_title_span").bind("click", function () {
                if($(this).next().is(":hidden")){
                    $(this).next().show();
                    $("#"+_ids+"_option_box_div span p").each(function(i,item){
                        if($("#"+_ids+"_select_title_span>span").text() == $(this).attr('title')){
                            $(this).addClass("option_selected");
                        }else{
                            $(this).removeClass("option_selected");
                        }
                    })
                }else{
                    $(this).next().hide();
                }
            });
            thisSelect.find("div#"+_ids+"_option_box_div>span>p").bind("click", function () {
                var $title=$("#"+_ids+"_select_title");
                $title.html($(this).html());
                $title.attr("valueId",$(this).attr("key"));
                $("#"+_ids+"_option_box_div").hide();
                if(typeof options.onSelect=="function"){
                    options.onSelect($(this).attr("key"),$(this).html());
                }
            });
        });
    };
    $.fn.selectAuto.methods={
        setValue:function(_this,data){
            var $title=$("#"+_this[0].id+"_select_title");
            var key,value;
            if(typeof data=="string"){
                key=data;value=data;
            }else if(typeof data=="object"){
                key=data.key;value=data.value;
            }
            $title.html(value);
            $title.attr("valueId",key);
            $(".option_box_div ul li p").each(function(i,item){
                if(value == $(this).attr('title')){
                    $(this).addClass("option_selected");
                }else{
                    $(this).removeClass("option_selected");
                }
            });
        },getValue:function(_this){//获取选中的值
            return  $("#"+_this[0].id+"_select_title").attr("valueId");
        },getText:function(_this){//获取选中的显示值
            return  $("#"+_this[0].id+"_select_title").text();
        },loadData:function(_this,data){
            _5d(_this[0].id,data);
        },clearData:function(_this){
            $("#"+_this[0].id+"_data_li").empty();
        },_5d:function(_i,_d){
            var html="";
            $.each(_d, function(key, d){
                html+=' <p key="'+ d.key+'" title="'+ d.value+'">'+ d. value+'</p>';
            });
            $("#"+_i+"_data_li").empty().append(html);
        }
    }
})(jQuery);

3,css代码
.select_title_span{
    display:inline-block; height:16px; padding:3px 10px 4px 10px; border:1px solid #cccccc;
    line-height:16px; outline:0 none;  background-color:#ffffff; cursor:pointer;
    margin-right:5px;position:absolute;
}
.select_icon{
    display:inline-block; width:14px;height:8px; overflow:hidden;
    background:url(../img/bg_dropd.png) no-repeat 0 0;
    float:right;
    margin-top: 4px;;
}
.option_selected{background-color:#008573; color:#ffffff;}
.option_box_div{ position:absolute;border: 1px solid #aaaaaa;background-color: #ffffff;}
.option_box_div span{float:left;*position:relative;overflow-x: hidden;overflow-y:scroll; }
.option_box_div span p{cursor:pointer;}

4,附件为所需素材和效果图
  • 大小: 2.8 KB
  • 大小: 2.3 KB
分享到:
评论

相关推荐

    jQuery插件-多选全选实时搜索下拉框

    jQuery下拉复选菜单,可实现多选全选功能,并提供下拉框内实时搜索

    jQuery实现动态加载select下拉列表项功能示例

    本文实例讲述了jQuery实现动态加载select下拉列表项功能。分享给大家供大家参考,具体如下: 需求说明: 以前使用的select下拉列表都是静态的,select 的option数据都是写死的。现在项目中的select需要根据不同的...

    jQuery动态产生select option下拉列表

    但其中没有实现动态产生select option下拉列表。 在jQuery环境之下使用创建jQuery对象来实现动态产生,那是很方便的事情。  在数据库中准备一些数据: 存储过程: 开发ASP.NET MVC,实现程序,少不了model: 现...

    自制的操作下拉列表框(SELECT)的三个jquery插件(ajax填充、联动、增加选项)

    自己做了三个专门用来操作下拉列表框(select)的jquery插件,一是通过ajax方式填充列表项的FillOptions, 二是下拉列表框的联动插件CascadingSelect,三是手动添加列表项的AddOption插件 具体说明请看这里 ...

    jQuery模拟select实现下拉菜单功能

    用jquery模拟一淘上面的搜索下拉的功能,利用css3做箭头的动画效果。 JS代码: /* * 模拟搜索下拉select * 默认调用方式:$(el).setSelect({ * optionList: [], //这里是下拉的选项,如['aa','bb'] * hidden...

    jQuery 获取select选中值及清除选中状态

    不会使用jQuery的后台真心伤不起,获取select下拉列表的值都让我搞了好一阵,然而并没有结束,恢复选中状态也是花了我半个小时。 这里先记一下省的以后会忘了。 获取jQuery中select下拉列表中的lang属性值: ...

    select多选下拉列表+模糊查询功能.rar

    select多选下拉列表+模糊查询功能,亲测完整,有ASP和HTML样例,代码完整。 &lt;!DOCTYPE html&gt; &lt;title&gt;Insert title here&lt;/title&gt; &lt;script src="jquery.min.js"&gt; &lt;script type="text/javascript" src="jquery....

    jquery下拉select控件操作方法分享(jquery操作select)

    JQuery获取和设置Select选项方法汇总如下: 代码: 代码如下:$(“#select_id”).change(function(){//code…}); //为Select添加事件,当选择其中一项时触发var checkText=$(“#select_id”).find(“option:...

    Angular下拉多选基于Bootstarp-select

    因为项目的需求,要求使用下拉多选来实现多个分类,网上搜了很多,包括jquery的select2,以及http://dotansimha.github.io/angularjs-dropdown-multiselect/docs/#/main里面的Basic Settings Example,可能是不会...

    jQuery实现Select下拉列表进行状态选择功能

    eg:在管理一篇博文时,因博文的管理有一列叫:状态的列,该列有诸多状态,如:正常,待审核,删除等… 此时,若使用Select下拉列表进行状态选择,并在选中具体项值后,通过Ajax异步提交,在效果及体验上就能得到更...

    自定义带图标的select,只需把图标地址写在option的title属性中即可

    基于Jquery的自定义下拉框JS插件。 带图标、分组等

    select下拉框插件jquery.editable-select详解

    项目中有个需求,下拉框既可以下拉选择,也可以手动填写 html代码 数据来源 &lt;select class=source&gt; &lt;option value=0&gt;人工导入&lt;/option&gt; &lt;option value=1&gt;数据服务平台&lt;/option&gt; &lt;/select&gt; js代码 ...

    基于jQuery的select下拉框选择触发事件实例分析

    本文实例讲述了基于jQuery的select下拉框选择触发事件实现方法。分享给大家供大家参考,具体如下: 我一直以来都认为,select 下拉框选择对选项 options 使用 onclick 注册事件即可,如下: &lt;select&gt; ...

    利用jquery操作select下拉列表框的代码

    例: 代码如下: &lt;select id=”sltList” name=”list”&gt; &lt;option value=”1″&gt;张三&lt;/... // 获取当前选中的option, text为文本, val则是option的值了 或 var item = $(“select[@name= list] option[@selected]

    JQuery中国省市区街道三级、四级联动下拉菜单插件

    中国省市区三级联动下拉菜单,省市区街道四级联动下拉菜单插件,三级插件取的值为地区编号或地区名,四级插件默认为地区编号,修改jquery.citys.js中的valueType为“name”,街道单独在代码中修改&lt;option&gt;标签的值,...

    jquery Ajax实现Select动态添加数据

    jquery Ajax实现Select动态添加数据,具体内容如下 1.背景  最近在工作中,遇到了一个关于select的问题。一般情况下,select下拉框中的数据都是固定的或者直接在jsp中读取列表值显示。但是,这次要实现select与别的...

    下拉框代码 下拉框特效 jquery下拉框代码

    &lt;script type="text/javascript" src="../../../jquery/jquery-1.7.1.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="jquery-ui.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="jquery.multiselect....

    jQuery中select与datalist制作下拉菜单时的区别浅析

    对于select来说,select的下拉菜单是供用户选择的,用户只能选择其中的选项不能自己添加。 但是datalist就不同了,datalist不仅可以供用户选择,用户还可以自己输入,而且datalist还可以达到模糊匹配的效果,使用很...

    jQuery实现监听下拉框选中内容发生改变操作示例

    本文实例讲述了jQuery实现监听下拉框选中内容发生改变操作。分享给大家供大家参考,具体如下: jQuery代码部分: [removed] $(document).ready(function(){ var defaultId = "${defaultSelected}"; var default...

Global site tag (gtag.js) - Google Analytics