如何使用JqueryAutocomplete在请求上发送多个参数

时间:2012-12-19 15:45:11

标签: jquery-ui jquery jquery-autocomplete

我正在开发一个返回JSON的API,但API调用有两个参数,“term”和“country”:

http://127.0.0.1:8000/internalapi/cidades/?country=PT&term=Barreir

如何修改此调用以支持额外的API参数“country”?

<script>
$(function() {
    function log( message ) {
        $( "<div>" ).text( message ).prependTo( "#log" );
        $( "#log" ).scrollTop( 0 );
    }

    $( "#city" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "http://127.0.0.1:8000/internalapi/cidades/",
                dataType: "jsonp",
                data: {
                    featureClass: "P",
                    style: "full",
                    maxRows: 12,
                    name_startsWith: request.term
                },
                success: function( data ) {
                    response( $.map( data.geonames, function( item ) {
                        return {
                            label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                            value: item.name
                        }
                    }));
                }
            });
        },
        minLength: 2,
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.label :
                "Nothing selected, input was " + this.value);
        },
        open: function() {
            $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
        },
        close: function() {
            $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
        }
    });
});
</script>

任何线索?

致以最诚挚的问候,


更新1:

用于选择国家/地区的HTML:

<div class="fieldWrapper">
    <label for="id_country">País:</label>
    <select id="id_country" name="country">
        <option selected="selected" value="">(Nothing)</option>
        <option value="PT">Portugal</option>
        <option value="ES">Espanha</option>
    </select>
</div>

1 个答案:

答案 0 :(得分:1)

您应该将参数添加到数据对象中:

            data: {
                featureClass: "P",
                style: "full",
                maxRows: 12,
                name_startsWith: request.term,
                country: $('#id_country').find(":selected").val()
            },