Jquery组合框禁用某些选项

时间:2014-10-03 14:49:48

标签: jquery combobox autocomplete

下面的代码就是我在标题中的内容。

效果很好。

现在我希望能够:

禁用或调暗一些(复数)选项,以便希望它们会变暗并且不会被点击。

任何想法都非常感激。

尝试过了

<option value="Three" disabled>Three</option>
<option value="Three" disabled="disabled">Three</option>

那些没有任何效果。

有效的代码如下:

<select id="combobox1">

<option value="">Select one...</option>
<option value="One">One</option>
<option value="Two">Two</option>
<option value="Three">Three</option>
<option value="Many-More">Many More</option>
</select>

<script type='text/javascript' src='http://code.jquery.com/ui/1.9.1/jquery-ui.min.js'></script>

<script type="text/javascript">
(function( jQuery ) {
    jQuery.widget( "ui.combobox", {
        _create: function() {
            var input,
                self = this,
                select = this.element.hide(),
                selected = select.children( ":selected" ),
                value = selected.val() ? selected.text() : "",
                wrapper = this.wrapper =jQuery( "<span>" )
                    .addClass( "ui-combobox" )
                    .insertAfter( select );             input = jQuery( "<input>" )
                .appendTo( wrapper )
.attr('placeholder', 'Go to .....')
.attr('onfocus', 'if(this.placeholder == "Go to .....") {this.placeholder = "";}')
.attr('onblur', 'if(this.placeholder == "") {this.placeholder = "Go to .....";}')
                .val( value )
                .addClass( "ui-state-default ui-combobox-input" )
                .autocomplete({
                    delay: 0,
                    minLength: 0,
                    source: function( request, response ) {
                        var matcher = new RegExp( jQuery.ui.autocomplete.escapeRegex(request.term), "i" );
                        response( select.children( "option" ).map(function() {

                            var text = jQuery( this ).text();
                            if ( this.value && ( !request.term || matcher.test(text) ) )
                                return {
                                    label: text.replace(
                                        new RegExp(
                                            "(?![^&;]+;)(?!<[^<>]*)(" +
                                            jQuery.ui.autocomplete.escapeRegex(request.term) +
                                            ")(?![^<>]*>)(?![^&;]+;)", "gi"
                                        ), "<strong>$1</strong>" ),
                                    value: text,
                                    option: this
                                };                          }) );
                    },
                    select: function( event, ui ) {
//alert( comboBoxThis.attr("id") );
//if( jQuery(this).attr("id") == "combobox1")
//document.location.href=ui.item.option.value;                          
                        ui.item.option.selected = true;
                        self._trigger( "selected", event, {
                            item: ui.item.option
                        });
                    },
                    change: function( event, ui ) {
                        if ( !ui.item ) {
                            var matcher = new RegExp( "^" + jQuery.ui.autocomplete.escapeRegex( jQuery(this).val() ) + "$", "i" ),
                                valid = false;
                            select.children( "option" ).each(function() {
                                if ( jQuery( this ).text().match( matcher ) ) {
                                    this.selected = valid = true;
                                    return false;
                                }
                            });
                            if ( !valid ) {
                                // remove invalid value, as it didn't match anything
                                jQuery( this ).val( "" );
                                select.val( "" );
                                input.data( "autocomplete" ).term = "";
                                return false;
                            }
                        }
                    }
                })
                .addClass( "ui-widget ui-widget-content ui-corner-left" );              
                input.data( "autocomplete" )._renderItem = function( ul, item ) {
                return jQuery( "<li></li>" )
                    .data( "item.autocomplete", item )
                    .append( "<a href='"+ item.option.value +"'>" + item.label + "</a>" )
                    .appendTo( ul );                };              jQuery( "<a>" )
                .attr( "tabIndex", -1 )
                .attr( "title", "Show All Items" )
                .appendTo( wrapper )
                .button({
                    icons: {
                        primary: "ui-icon-triangle-1-s"
                    },
                    text: false
                })
                .removeClass( "ui-corner-all" )
                .addClass( "ui-corner-right ui-combobox-toggle" )
                .click(function() {
                    // close if already visible
                    if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
                        input.autocomplete( "close" );
                        return;
                    }                       
                    // work around a bug (likely same cause as #5265)
                    jQuery( this ).blur();                      
                    // pass empty string as value to search for, displaying all results
                    input.autocomplete( "search", "" );
                    input.focus();
                });
        },          destroy: function() {
            this.wrapper.remove();
            this.element.show();
            jQuery.Widget.prototype.destroy.call( this );
        }
    });
})( jQuery );   jQuery(function() {
    jQuery( "#combobox, #combobox1" ).combobox();
    jQuery( "#toggle" ).click(function() {
        jQuery( "#combobox, #combobox1" ).toggle();
    });
jQuery(document).ready(function(){
jQuery('.ui-autocomplete:first').addClass('hCombo');

    jQuery('.hCombo li a').live('click',function(){
        var cId = jQuery('.demo').find('select ').attr('id');
//            alert( jQuery(this).attr('href') );
        document.location.href= jQuery(this).attr('href');

    });
});

});//alert(jQuery('.demo').html());
</script>

0 个答案:

没有答案