【问题标题】:Hide value on JQuery auto complete from XML从 XML 隐藏 JQuery 自动完成的值
【发布时间】:2011-10-08 22:20:09
【问题描述】:

我正在处理从 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 中搜索并在建议和输入字段中显示它。 所以我输入“United Kingdom”,然后我在建议中看到“London, GB”,并在选择后作为输入字段中的值。

有可能吗?

【问题讨论】:

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


    【解决方案1】:

    当然,在您的退货中添加标签:

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

    【讨论】:

    • 我尝试过,但以这种方式搜索“英国”不会返回任何结果。它仍然适用于“London”或“GB”等查询
    • @Luca:您使用的是哪个自动完成功能?您通常可以设置一个“标签”属性而不是设置值。
    • 答案已编辑。除了值和 id 之外,您还可以在该结果数组中使用“标签”属性。
    • 也试过这个,但现在它让我只在标签值 ("name") 内搜索 ... value: $( "name", this ).text() + ", " + ( $.trim( $( "countryName", this ).text() ) || "(unknown country)" ) + ", " + $( "countryCode", this ).text() , label: $( "名称”,这个 ).text(),...
    • 我需要在一个我不想在建议和输入字段中显示的 xml 节点中进行搜索。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 2010-11-15
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    相关资源
    最近更新 更多