devbridge / jQuery-Autocomplete

Ajax Autocomplete for jQuery allows you to easily create autocomplete/autosuggest boxes for text input fields
https://www.devbridge.com/sourcery/components/jquery-autocomplete/
Other
3.56k stars 1.66k forks source link

share:realize combobox function Only selected?【已解决】 #731

Closed fmpoffice closed 6 years ago

fmpoffice commented 6 years ago

how can to only fill in values from the drop-down box ?

Realization effect: http://jqueryui.com/resources/demos/autocomplete/combobox.html

       function searchCompanyNames(){
        if( typeof ($.fn.autocomplete) === 'undefined'){ return; }
        $('.companyNames').autocomplete({
            serviceUrl: '/search/searchCompanyAutocompleter',
            /* onSearchComplete: function(query, suggestions) {       
                console.log('onSearchComplete...');
            }, */
            onSelect: function (suggestion) {
                console.log('onSelect...' + suggestion.value);
                //companyNames = suggestion.value;
                $(".companyNames").width(textWidth(suggestion.value+"xxxxxx"));
            },width:"400px"
        });
    }

    $(".companyNames").on('change', function() {
        console.log("companyNames on change : " + $(this).val());
        var id = this.id;
          setTimeout(function(){    //设时延迟0.5s执行
              $.ajax({
                    async: true,
                    url: "/search/searchCompanyAutocompleter", 
                    data: "query="+$("#" + id).val(), 
                    success: function(obj){
                        console.log(obj.suggestions);
                        var b = false;
                        $.each(obj.suggestions, function(i,val){      
                            if(val == obj.query){
                                b = true;
                                return false;
                            }else{
                                b = false;
                            }
                        });   
                        if(!b){
                            $("#" + id).val("");
                            show_stack_bottomright("error","请选择已有的公司!没有则请先添加!",5500);
                        }
                     //console.log(json);
                  }
                });
          },400);
    }); 
tkirda commented 6 years ago

This plugin only supports suggestions.

If you are looking to have something similar to dropdown then check out React Select

fmpoffice commented 6 years ago

thank you

Shnoulle commented 6 years ago

for information, i use this snippet (with jquery)

        $('#autocomplete').on("keyup keydown",function(e) {
            var code = e.keyCode || e.which;
            if (code != '9' && code != '13' ) {
                e.preventDefault();
            }
        });