How to dynamic filter options of <select > with jQuery?

前端 未结 12 598
轮回少年
轮回少年 2020-11-29 18:54



        
12条回答
  •  隐瞒了意图╮
    2020-11-29 19:06

    I had a similar problem to this, so I altered the accepted answer to make a more generic version of the function. I thought I'd leave it here.

    var filterSelectOptions = function($select, callback) {
    
        var options = null,
            dataOptions = $select.data('options');
    
        if (typeof dataOptions === 'undefined') {
            options = [];
            $select.children('option').each(function() {
                var $this = $(this);
                options.push({value: $this.val(), text: $this.text()});
            });
            $select.data('options', options);
        } else {
            options = dataOptions;
        }
    
        $select.empty();
    
        $.each(options, function(i) {
            var option = options[i];
            if(callback(option)) {
                $select.append(
                    $('

提交回复
热议问题