Using jQuery Autocomplete combobox

I am trying to use this jQuery code in my program: http://jqueryui.com/demos/autocomplete/#combobox

Basically this is a combo box that has an autocomplete field and a drop-down button.

When I try to use my combo box inside the form tags, it does not work properly - the drop-down list button keeps sending the form when I just want to search for the values.

The source code from the example is as follows:

$( "<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();

            });

Any help appreciated :).

+3
source share
1 answer

I found a solution, just need to make two small changes:

.click(function() {
// close if already visible
if (input.autocomplete("widget").is(":visible")) {
    input.autocomplete("close");
    return false; // CHANGE 1
}
// pass empty string as value to search for, displaying all results
input.autocomplete("search", "");
input.focus();
return false; // CHANGE 2
});
+4
source

Source: https://habr.com/ru/post/1760243/


All Articles