ss
doum
2025-09-19 566c35444def62d13f0c76c3a8e642cd234b2b45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
 * selectFilter  --v1.1            
 * 
 * author: 840399345@qq.com
 * 
 * $(el).selectFilter(options);
 * 
 * options={
 *  callBack : function (res){}  // 返回选中的值 进行事件操作
 * }
 * 
 * 也可以放在表单直接获取  select标签的 值
 * 
 **/
 
;jQuery.fn.selectFilter = function (options){
    var defaults = {
        callBack : function (res){}
    };
    var ops = $.extend({}, defaults, options);
    var selectList = $(this).find('select option');
    var that = this;
    var html = '';
    
    // 读取select 标签的值
    html += '<ul class="filter-list">';
    
    $(selectList).each(function (idx, item){
        var val = $(item).val();
        var valText = $(item).html();
        var selected = $(item).attr('selected');
        var disabled = $(item).attr('disabled');
        var isSelected = selected ? 'filter-selected' : '';
        var isDisabled = disabled ? 'filter-disabled' : '';
        if(selected) {
            html += '<li class="'+ isSelected +'" data-value="'+val+'"><a title="'+valText+'">'+valText+'</a></li>';
            $(that).find('.filter-title').val(valText);
        }else if (disabled){
            html += '<li class="'+ isDisabled +'" data-value="'+val+'"><a>'+valText+'</a></li>';
        }else {
            html += '<li data-value="'+val+'"><a title="'+valText+'">'+valText+'</a></li>';
        };
    });
    
    html += '</ul>';
    $(that).append(html);
    $(that).find('select').hide();
    
    //点击选择
    $(that).on('click', '.filter-text', function (){
        $(that).find('.filter-list').slideToggle(100);
        $(that).find('.filter-list').toggleClass('filter-open');
        $(that).find('.icon-filter-arrow').toggleClass('filter-show');
    });
    
    //点击选择列表
    $(that).find('.filter-list li').not('.filter-disabled').on('click', function (){
        var val = $(this).data('value');
        var valText =  $(this).find('a').html();
        $(that).find('.filter-title').val(valText);
        $(that).find('.icon-filter-arrow').toggleClass('filter-show');
        $(this).addClass('filter-selected').siblings().removeClass('filter-selected');
        $(this).parent().slideToggle(50);
        for(var i=0; i<selectList.length; i++){
            var selectVal = selectList.eq(i).val();
            if(val == selectVal) {
                $(that).find('select').val(val);
            };
        };
        ops.callBack(val); //返回值
    });
    
    //其他元素被点击则收起选择
    $(document).on('mousedown', function(e){
        closeSelect(that, e);
    });
    $(document).on('touchstart', function(e){
        closeSelect(that, e);
    });
    
    function closeSelect(that, e) {
        var filter = $(that).find('.filter-list'),
            filterEl = $(that).find('.filter-list')[0];
        var filterBoxEl = $(that)[0];
        var target = e.target;
        if(filterEl !== target && !$.contains(filterEl, target) && !$.contains(filterBoxEl, target)) {
            filter.slideUp(50);
            $(that).find('.filter-list').removeClass('filter-open');
            $(that).find('.icon-filter-arrow').removeClass('filter-show');
        };
    }
};