【问题标题】:How do I get this jQuery autocomplete code to work in FireFox?如何让这个 jQuery 自动完成代码在 FireFox 中工作?
【发布时间】:2011-05-29 19:36:58
【问题描述】:

大家好, 我有这个 jQuery 代码,它可以在 Chrome 和 Safari 上运行,但不能在 IE 或 FireFox 上运行。它有什么问题?

<script>

$(document).ready(function() {
    var myArr = [];

    $.ajax({
        type: "GET",
        url: "airports.xml", 
        dataType: "xml",
        success: parseXml,
        complete: setupAC,
        failure: function(data) {
            alert("XML File could not be found");
            }
    });

    function parseXml(xml)
    {
        //find every query value
        $(xml).find("airport").each(function()
        {
            myArr.push($(this).attr("label"));
        }); 
    }

    function setupAC() {
        $("input#depart_from").autocomplete({
                source: myArr,
                minLength: 1,
                select: function(event, ui) {
                    $("input#depart_from").val(ui.item.value);
                    $("#submitform").submit();
                }
        });
    }
});

</script>

这是我的输入元素

<input id="depart_from" type="text" name="depart_from" placeholder="Depart from"/>

有什么建议吗?

【问题讨论】:

  • 您遇到什么错误?究竟是什么不起作用?
  • 这是奇怪的部分。我在 Firebug 中没有收到任何错误。真的很奇怪。下拉自动完成列表不显示。它显示在 Safari 和 Chrome 中。

标签: jquery xml jquery-ui jquery-autocomplete


【解决方案1】:

好的,修改后的答案。将 dataType 更改为 html 并修复 xml 文档中的错误:

$(document).ready(function() {
var myArr = [];
function parseXml(xml)
{
    //find every query value
    $(xml).find("airport").each(function()
    {
        myArr.push($(this).attr("label"));
    }); 
}

function setupAC() {
    $("input#depart_from").autocomplete({
            source: myArr,
            minLength: 1,
            select: function(event, ui) {
                $("input#depart_from").val(ui.item.value);
                $("#submitform").submit();
            }
    });
}

$.ajax({
    type: "GET",
    url: "airports.xml", 
    dataType: "html",
    success: parseXml,
    complete: setupAC,
    failure: function(data) {
        alert("XML File could not be found");
        }
});


});

【讨论】:

  • 没有帮助!还有其他建议吗?
  • 我为它做了一个 jsfiddle。您可以发送指向示例 xml 文件的链接吗?我可以将其插入并查看它是否显示任何问题。
  • 当我尝试在 FF 中运行时遇到 xml 解析错误。您在 Firebug 中看到此错误吗?这就是我得到的:XML 解析错误:找不到元素位置:moz-nullprincipal:{dd117028-da50-7b48-9a90-51f90c48ea9e} 第 1 行,第 1 列:
  • 我想我找到了:label='Coeur d'Alene'
  • 在 Firebug 中看不到!这很奇怪。也许xml文件有问题。会这样吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 2015-02-05
相关资源
最近更新 更多