如何使用Javascript停止打破多个选择框

时间:2013-10-28 18:17:23

标签: jquery forms select webforms drop-down-menu

我使用jquery创建了自定义选择下拉列表。当一个选择框出现时,它工作得很好......但是当同一页面上出现两个选择框时,一切都会崩溃,只有最后一个选择元素才能正常工作但是它。

适用于一个的代码:

$(document).ready(function(){

 var select = $('select.selectForm');

    var selectBoxContainer = $('<div>',{
       "class"     : 'selectContainer',
        html        : '<div class="selectBox"></div>'
    });

    var dropDown = $('<ul>',{
        "class"     : 'selectDropDown'
    });

    var selectBox = selectBoxContainer.find('.selectBox');


    //Loop though the options of the original select element
    select.find('option').each(function(i){
        var option = $(this);

        //sets default text to first option in the select
        if( i !== select.prop("selectedIndex") ){           
            selectBox.html( option.text() );
        }

        if( option.data('skip') ){
            return true;
        }

        //Creating a dropdown list from the items in out select element using the option text
        var li = $('<li>',{
            html : option.text()
        });

        li.on("click",function(){
            selectBox.html( option.text() );
            dropDown.trigger('hide'); //might be dropDown.trigger('hide');

            //also change the original select element
            select.val( option.val() );

            return false;
        });

        //add list item to the dropdown menu
        dropDown.append(li);

    });//end of select find

    //Adding dropdown to DOM
    selectBoxContainer.append(dropDown.hide()); //adding dropDown ul to DOM within the selectContainer div    
    select.hide().after(selectBoxContainer); //Hides original select element and inserts ul containder after it



    dropDown.bind('show',function(){
        if(dropDown.is(':animated')){
            return false;
        }
        selectBox.addClass('expanded');
        dropDown.slideDown();

    }).bind('hide',function(){

        if(dropDown.is(':animated')){
            return false;
        }

        selectBox.removeClass('expanded');
        dropDown.slideUp();

    }).bind('toggle',function(){

        if(selectBox.hasClass('expanded')){
            dropDown.trigger('hide');
        } else{
            dropDown.trigger('show');
        }
    });

    selectBox.on('click',function(){
        dropDown.trigger('toggle');
        return false;
    });

    $(document).on("click",function(){
        dropDown.trigger('hide');
    });


 });//document ready end

如何使用一个: http://jsfiddle.net/im_cr/TyPSX/

如何突破两个: http://jsfiddle.net/im_cr/TyPSX/5/

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:1)

我猜你正在寻找更像这样的东西:

$.fn.selectForm = function () {
    return this.each(function () {
        var selectBox = $('<div />', {
                'class': 'selectBox'
            }),
            selectBoxContainer = $('<div />', {
                "class": 'selectContainer'
            }),
            dropDown = $('<ul />', {
                "class": 'selectDropDown'
            });

        selectBoxContainer.append(selectBox);

        $(this).find('option').each(function (i, option) {
            //sets default text to first option in the select
            if (i !== $(this).prop("selectedIndex")) {
                selectBox.html($(option).text());
            }

            if ($(option).data('skip')) {
                return true;
            }

            //Creating a dropdown list from the items in out select element using the option text
            var li = $('<li>', {
                html: $(option).text(),
                on: {
                    click: function () {
                        selectBox.html($(option).text());
                        dropDown.trigger('hide'); //might be dropDown.trigger('hide');
                        select.val(option.value);
                        return false;
                    }
                }
            });
            dropDown.append(li);
        }); //end of select find

        selectBoxContainer.append(dropDown.hide());
        $(this).hide().after(selectBoxContainer);

        dropDown.bind('show', function () {
            if (dropDown.is(':animated')) {
                return false;
            }
            selectBox.addClass('expanded');
            dropDown.slideDown();
        }).bind('hide', function () {
            if (dropDown.is(':animated')) {
                return false;
            }
            selectBox.removeClass('expanded');
            dropDown.slideUp();
        }).bind('toggle', function () {
            if (selectBox.hasClass('expanded')) {
                dropDown.trigger('hide');
            } else {
                dropDown.trigger('show');
            }
        });

        selectBox.on('click', function () {
            dropDown.trigger('toggle');
            return false;
        });

        $(document).on("click", function () {
            dropDown.trigger('hide');
        });
    });
}

$(function () {
    $('select.selectForm').selectForm();
});

FIDDLE