开发者

Hide value on JQuery auto complete from XML

I'm working on JQuery auto complete from XML.

I wish to search in one XML node without show the value in the callback, but I'm not able to find a solution.

This is the script

    <script>
$(function() {
    $.ajax({
        url: "london.xml",
        dataType: "xml",
        success: function( xmlResponse ) {
            var data = $( "geoname", xmlResponse ).map(function() {
                return {
                    value: $( "nam开发者_StackOverflowe", this ).text() + ", " +
                        ( $.trim( $( "countryName", this ).text() ) || "(unknown country)" ) + ", " +
                        $( "countryCode", this ).text() 
                        ,
                        id: $( "geonameId", this ).text()
                };
            }).get();
            $( "#birds" ).autocomplete({
                source: data,
                minLength: 0,
                select: function( event, ui ) {
                    $('#hide').val(ui.item.id)
                }
            });
        }
    });
});
</script>

This is an xml node

<geoname>
<name>London</name>
<lat>51.5084152563931</lat>
<lng>-0.125532746315002</lng>
<geonameId>2643743</geonameId>
<countryCode>GB</countryCode>
<countryName>United Kingdom</countryName>
<fcl>P</fcl>
<fcode>PPLC</fcode>
</geoname>

At the moment, if I type "Kingdom" and click on the result, the input field fills with "London, United Kingdom, GB". What I wish is to search in countryName wiyhout showing it inside the suggestion and in the input field. So I type "United Kingdom" and I see "London, GB" inside the suggestion and as value in the input field once selected.

Is it possible?


Sure, add a label into your return:

label: $( "name", this ).text() + ", " + $( "countryCode", this ).text() 

Your script then becomes:

<script>
    $(function() {
        $.ajax({
            url: "london.xml",
            dataType: "xml",
            success: function( xmlResponse ) {
                var data = $( "geoname", xmlResponse ).map(function() {
                    return {
                        value: $( "name", this ).text() + ", " +
                            ( $.trim( $( "countryName", this ).text() ) ||
                            "(unknown country)" ) + ", " +
                            $( "countryCode", this ).text(),
                        label: $("name", this).text() + ", " +
                            $("countryCode", this).text(),
                        id: $( "geonameId", this ).text()
                };
            }).get();
            $( "#birds" ).autocomplete({
                source: data,
                minLength: 0,
                select: function( event, ui ) {
                    $('#hide').val(ui.item.id)
                }
            });
        }
    });
});

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜