jQuery UI AutoComplete: Only allow selected valued from suggested list

前端 未结 9 1938
误落风尘
误落风尘 2020-11-30 22:48

I am implementing jQuery UI Autocomplete and am wondering if there is any way to only allow a selection from the suggested results that are

9条回答
  •  粉色の甜心
    2020-11-30 23:40

    Ajax submission and handling

    This will be of use to some of you out there:

    $('#INPUT_ID').autocomplete({
        source: function (request, response) {
            $.ajax({
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                url: autocompleteURL,
                data: "{'data':'" + $('INPUT_ID').val() + "'}",
                dataType: 'json',
                success: function (data) {
                    response(data.d);
                },
                error: function (data) {
                    console.log('No match.')
                }
            });
        },
        change: function (event, ui) {
            var opt = $(this).val();
    
            $.ajax({
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                url: autocompleteURL,
                data: "{'empName':'" + name + "'}",
                dataType: 'json',
                success: function (data) {
                    if (data.d.length == 0) {
                        $('#INPUT_ID').val('');
                        alert('Option must be selected from the list.');
                    } else if (data.d[0] != opt) {
                        $('#INPUT_ID').val('');
                        alert('Option must be selected from the list.');
                    }
                },
                error: function (data) {
                    $(this).val('');
                    console.log('Error retrieving options.');
                }
            });
        }
    });
    

提交回复
热议问题