从XML中隐藏JQuery自动完成的值

时间:2011-07-19 21:41:01

标签: jquery xml jquery-ui search jquery-autocomplete

我正在从XML开始自动完成JQuery。

我希望在一个XML节点中搜索而不显示回调中的值,但我无法找到解决方案。

这是脚本

    <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() 
                        ,
                        id: $( "geonameId", this ).text()
                };
            }).get();
            $( "#birds" ).autocomplete({
                source: data,
                minLength: 0,
                select: function( event, ui ) {
                    $('#hide').val(ui.item.id)
                }
            });
        }
    });
});
</script>

这是一个xml节点

<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>

目前,如果我输入“Kingdom”并单击结果,则输入字段将填充“London,United Kingdom,GB”。 我希望在countryName中搜索wiyhout在建议和输入字段中显示它。 所以我输入“英国”,我在建议中看到“伦敦,GB”,并在选择后在输入字段中显示为值。

有可能吗?

1 个答案:

答案 0 :(得分:0)

当然,在您的回报中添加一个标签:

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

然后您的脚本变为:

<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)
                }
            });
        }
    });
});

相关问题