0

我目前有一个 ajax 脚本,可以动态构建两个启用了 Chosen 插件的选择框:https ://github.com/harvesthq/chosen 。我已经缩小了第 9 行是启用 Chosen Plugin 的原因,据我所知,脚本会动态地为选择构建标记。如何拆分此动态选择标记的构建,以便第一个选择不使用 Chosen 插件启用?

$(function(){

    var questions = $('#questions');

    function refreshSelects(){
        var selects = questions.find('select');

        // Improve the selects with the Chosen plugin
        selects.chosen({ disable_search_threshold: true });

        // Listen for changes
        selects.unbind('change').bind('change',function(){

            // The selected option
            var selected = $(this).find('option').eq(this.selectedIndex);
            // Look up the data-connection attribute
            var connection = selected.data('connection');


            // Removing the li containers that follow (if any)
            selected.closest('#questions li').nextAll().remove();

            if(connection){
                fetchSelect(connection);
            }

        });
    }

    var working = false;

    function fetchSelect(val){

        if(working){
            return false;
        }
        working = true;

        $.getJSON('citibank.php',{key:val},function(r){

            var connection, options = '';

            switch (r.type) {
                case 'select':
                    $.each(r.items,function(k,v){
                        connection = '';
                        if(v){
                            connection = 'data-connection="'+v+'"';
                        }

                        options+= '<option value="'+k+'" '+connection+'>'+k+'</option>';
                    });

                    if(r.defaultText){

                        // The chose plugin requires that we add an empty option
                        // element if we want to display a "Please choose" text

                        options = '<option></option>'+options;
                    }

                    // Building the markup for the select section

                    $('<li>\
                        <p>'+r.title+'</p>\
                        <select data-placeholder="'+r.defaultText+'">\
                            '+ options +'\
                        </select>\
                        <span class="divider"></span>\
                    </li>').appendTo(questions);

                    refreshSelects();
                    break;
                case 'html':
                    $(r.html).appendTo(questions);
                    break;
            }

            working = false;
        });

    }

    $('#preloader').ajaxStart(function(){
        $(this).show();
    }).ajaxStop(function(){
        $(this).hide();
    });

    // Initially load the product select
    fetchSelect('callTypeSelect');
});
4

1 回答 1

1

:not:first

selects.filter(":not(:first)").chosen({ disable_search_threshold: true });

或者

selects.not(":first").chosen({ disable_search_threshold: true });
于 2012-06-28T15:09:13.037 回答