jquery autocomplete field that REQUIRES a selected value?

后端 未结 3 1438
一向
一向 2021-01-28 02:01

I need some jquery plugin as they use it on most flight booking sites where you have to choose your departure/arrival airport from a combolist.

i had a look around, but

3条回答
  •  余生分开走
    2021-01-28 02:32

    The plugin by jQuery UI actually allows you to send through a text value and an integer as return types.

    Here is the general code I use for auto completes.

    $("#c_where").autocomplete({
        autoFill: true,
        mustMatch: true,
        selectFirst: true,
        source: function(request, response) {
            $.ajax({
                type: "post",
                url: "inc/ajax/json/hotels.php", // Upload this aswell
                data: {
                    maxRows: 12,
                    term: request.term
                },
                success: function(data) {
                    response($.map(JSON.parse(data), function(item) {
                        return {
                            label: item.name,
                            value: item.name,
                            id: item.hid
                        }
                    }));
                }
            });
        },
        change: function(event, ui) {
            /*if ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0) {
                $(this).val('');
            }*/
            if (!ui.item) {
                $(this).val('');
            }
        },
        select: function(event, ui) {
            $('#c_where').data("id", ui.item.id);
        }
    }).live('keydown', function (e) {
        var keyCode = e.keyCode || e.which;
        if((keyCode == 9 || keyCode == 13) && ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0)) {
            $(this).val($(".ui-autocomplete li:visible:first").text());
        }
    });
    

提交回复
热议问题