无法获取jQuery变量以将数据引入<div> id </div>

时间:2014-02-15 06:26:23

标签: jquery html attr

我正在通过jQuery读取XML文件,并希望将值(var_address)传递给属性 - “数据地址”。但是,我无法做到这一点。我认为我的jQuery语句中存在一个问题,因为“var_address”没有获取任何值。有人可以帮忙吗?这就是我所拥有的:

提前感谢。

jQuery的:

$(document).ready(function () {
    $("#paneldetails").append("<ul></ul>");
    $.ajax({
        type: "GET",
        url: "/datafile.xml",
        dataType: "xml",
        success: function (xml) {
            $(xml).find('Table1').each(function () {
                var BuildingNumber = $(this).find('BuildingNumber').text();
                var StreetName = $(this).find('StreetName').text();
                var StreetType = $(this).find('StreetType').text();                    
                var City = $(this).find('City').text();
                var State = $(this).find('State').text();
                var ZipCode = $(this).find('ZipCode').text()
                $('div#var_address').attr('data-address', BuildingNumber + '+' + StreetName + '+' + StreetType + '+' + ',' + '+' + City + '+' + ',' + State + '+' + ' ' + ZipCode);  // transfer var0023 to HTML file
                return false;
            }
        },
        error: function () {
            alert("Error found in the data associated with this record");
        }
    });
});

HTML:

 <div class="client" id="var_address" data-address="theaddress" ></div>

XML

<?xml version="1.0" encoding="UTF-8"?>
<DocumentElement>
<Table1> 
    <Business_x0020_Category>BusinessEssentials</Business_x0020_Category>
    <PanelID>0001</PanelID>
    <Business_x0020_ID>1001</Business_x0020_ID>
    <BusinessName>Acme Tech</BusinessName>
    <BuildingNumber>2173</BuildingNumber>
    <StreetName>Salk</StreetName>
    <StreetType>Ave</StreetType>
    <Suite-Unit_x0020_Number>Suite 600</Suite-Unit_x0020_Number>
    <City>Carlsbad</City>
    <State>CA</State>
    <ZipCode>92008</ZipCode>
    <Telephone>XXX-XXX-XXXX</Telephone>
    <Email>XXXXX@XXXXXX.XXX</Email>
</Table>

1 个答案:

答案 0 :(得分:0)

您的$.each()未正确关闭且xml Table1未关闭,请尝试使用,

<强> XML

<?xml version="1.0" encoding="UTF-8"?>
<DocumentElement>
<Table1> 
    ...
</Table1><!-- Table1 not Table -->

<强> SCRIPT

success callback

$(xml).find('Table1').each(function () {
    ......
    return false;
});<-- you missed closing )