jQuery combobox/autocomplete but editable

前端 未结 4 831
野趣味
野趣味 2021-02-11 00:50

I\'m using the jQuery Autocomplete but I need it to be editable. What I mean is that if a value isn\'t in the list, I need to capture the value they entered. Using the example

4条回答
  •  自闭症患者
    2021-02-11 01:37

    What I do myself when using the jQuery UI autocomplete combobox is to declare the combobox widget like this:

    (function( $ ) {
        $.widget( "ui.combobox", {
            // default options
            options: {
                strict: false
            },
            _create: function() {
                var self = this,
                    select = this.element.hide(),
                    selected = select.children( ":selected" ),
                    value = selected.val() ? selected.text() : "",
                    strict = this.options.strict;
    
                var input = this.input = $( "" )
                    .insertAfter( select )
                    .val( value )
                    .autocomplete({
                        delay: 0,
                        minLength: 0,
                        source: function( request, response ) {
                            var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
                            response( select.children( "option" ).map(function() {
                                var text = $( this ).text();
                                if ( this.value && ( !request.term || matcher.test(text) ) )
                                    return {
                                        label: text.replace(
                                            new RegExp(
                                                "(?![^&;]+;)(?!<[^<>]*)(" +
                                                $.ui.autocomplete.escapeRegex(request.term) +
                                                ")(?![^<>]*>)(?![^&;]+;)", "gi"
                                            ), "$1" ),
                                        value: text,
                                        option: this
                                    };
                            }) );
                        },
                        select: function( event, ui ) {
                            ui.item.option.selected = true;
                            self._trigger( "selected", event, {
                                item: ui.item.option
                            });
                        },
                        autocomplete : function(value) {
                            this.element.val(value);
                            this.input.val(value);
                        },
                        change: function( event, ui ) {
                            if ( !ui.item ) {
                                var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
                                    valid = false;
                                select.children( "option" ).each(function() {
                                    if ( this.value.match( matcher ) ) {
                                        this.selected = valid = true;
                                        return false;
                                    }
                                });
                                if ( !valid ) {
                                    // if strict is true, then unmatched values are not allowed
                                    if ( strict ) {
                                        // remove invalid value, as it didn't match anything
                                        $( this ).val( "" );
                                        select.val( "" );
                                    }
                                    return false;
                                }
                            }
                        }
                    })
                    .addClass( "ui-widget ui-widget-content ui-corner-left" );
    
                input.data( "autocomplete" )._renderItem = function( ul, item ) {
                    return $( "
  • " ) .data( "item.autocomplete", item ) .append( "" + item.label + "" ) .appendTo( ul ); }; this.button = $( "" ) .attr( "tabIndex", -1 ) .attr( "title", "Show All Items" ) .insertAfter( input ) .button({ icons: { primary: "ui-icon-triangle-1-s" }, text: false }) .removeClass( "ui-corner-all" ) .addClass( "ui-corner-right ui-button-icon" ) .click(function() { // close if already visible if ( input.autocomplete( "widget" ).is( ":visible" ) ) { input.autocomplete( "close" ); return; } // pass empty string as value to search for, displaying all results input.autocomplete( "search", "" ); input.focus(); }); } }); })( jQuery );

    That would make it accept other options as valid values, and you'll just have to use:

    $("#combobox").combobox({
        strict: true
    });
    

    To make it work as default just in case you need it.

提交回复
热议问题