如何使用jquery multiselect显示组名以及下拉列表项?

时间:2012-10-09 07:03:11

标签: javascript jquery asp.net jquery-ui

您好我正在使用http://www.erichynds.com/jquery/jquery-ui-multiselect-widget 用于多选下拉选项组的插件。

我想显示组名以及项目。请找到我用于相同的代码。

<script type="text/javascript">
    $(document).ready(function () {
        $(".multiselect").multiselect({
            selectedList: 10,
            noneSelectedText: 'Select Options',
            header: false,
            show: ['slide', 100],
            hide: ['explode', 100], //explode,bounce
            height: 200,
            multiple: true,
            autoOpen: false,
            position: {
                my: 'center',
                my: 'top',
                at: 'center',
                at: 'bottom'
            }
        });

        var target = $('#<%=hdnftest.ClientID %>');
        var a = "";
        $(".multiselect").multiselect().bind("multiselectclick multiselectcheckall multiselectuncheckall", function (event, ui) {
            var checkedValues = $.map($(this).multiselect("getChecked"), function (input) {
                return (input.value + ':' + input.title);
            });
            target.val(
            checkedValues.length
                ? checkedValues.join(', ')
                : 'Please select a checkbox'
        );

        }).triggerHandler("multiselectclick");
        $(".multiselect").multiselect("close");

    });

      </script>

    <select class="multiselect" multiple="multiple">
         <optgroup label="EAST">
             <option>Testing1</option>
                <option>Testing2</option>
                <option>Testing3</option>
                <option>Testing4</option>
            </optgroup>
            <optgroup label="NORTH">
                <option>Testing5</option>
                <option>Testing6</option>
                <option>Testing7</option>
                <option>Testing8</option>
            </optgroup>
            <optgroup label="SOUTH">
                <option>Testing9</option>
                <option>Testing10</option>
                <option>Testing11</option>
                <option>Testing12</option>
            </optgroup>
       </select>

1 个答案:

答案 0 :(得分:0)

您可以处理多选小部件的创建事件并更改其中复选框的标签:

$(".multiselect").multiselect({
    selectedList: 10,
    noneSelectedText: 'Select Options',
    header: false,
    show: ['slide', 100],
    hide: ['explode', 100],
    //explode,bounce
    height: 200,
    multiple: true,
    autoOpen: false,
    position: {
        my: 'center',
        my: 'top',
        at: 'center',
        at: 'bottom'
    },
    create: function(event, ui) {
        var checkBoxes = $(this).multiselect("widget").find(":checkbox");
        checkBoxes.each(function() {
            var caption = $(this).next("span");
            var groupText = $(this).parents("li").prevAll(".ui-multiselect-optgroup-label:first").text();
            caption.text(groupText + " - " + caption.text());
        });
    }
});​

JSFiddle link