jQuery combobox/autocomplete but editable

前端 未结 4 815
野趣味
野趣味 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:17

    Using the cleaner solution from darkajax, and the following line of code:

    var valu = $("#combobox").parent().children()[1].value;
    

    I was able to get the value of the combobox.

    0 讨论(0)
  • 2021-02-11 01:26

    In your

    source: function(request, response)
    {
    

    you can access the request.term = what was typed.

    0 讨论(0)
  • 2021-02-11 01:36

    I have no clue if this will help anyone, but by giving the input a name, the value was submitted.... (I removed the code that removed invalid entries, of course)

    var input = this.input = $("<input name='somehthing'>")
    
    0 讨论(0)
  • 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 = $( "<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"
                                            ), "<strong>$1</strong>" ),
                                        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 $( "<li></li>" )
                        .data( "item.autocomplete", item )
                        .append( "<a>" + item.label + "</a>" )
                        .appendTo( ul );
                };
    
                this.button = $( "<button type=\"button\" class=combo_button>&nbsp;</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.

    0 讨论(0)
提交回复
热议问题