如何让这个jQuery自动完成代码在FireFox中运行?

时间:2011-05-29 19:36:58

标签: jquery xml jquery-ui jquery-autocomplete

嘿伙计们, 我有这个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"/>

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

好的,修改后的答案。将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");
        }
});


});