-1

我正在尝试使用 $.getJSON 填充我数据库中最后一条记录中的数据。GET 返回 JSON,但我无法获取数据来填充字段。我完全是新手。

$.getJSON("loadloads.php", function (data) {
    document.getElementById("LoadNumber").innerHTML = data.LoadNumber
});

这是返回的 JSON,从 firebug 复制。

[{
    "value": {
        "LoadNumber": "227303",
        "OrderDate": "2013-04-09",
        "ShipDate": "2013-04-09",
        "ShipTime": "12:00:00",
        "DeliveryDate": "2013-04-10",
        "DeliveryTime": null,
        "PurchaseOrderNumber": "0002323803",
        "CustomerId": "200540",
        "Rate": "801.08",
        "EmployeeId1": "1",
        "EmployeeId2": "1",
        "CarrierId": "201383",
        "CarrierRate": "700",
        "Tarp": "NO",
        "Status": "Dispatched",
        "Notes": "Pls check in as an All Points Inc. carrier.  V-boards reg.  Shipper tracking we need Proof of delivery faxed or called in A.S.A.P (within 24 hours)  Need printed name of person signing for load.  Must haul 48K min.",
        "LoadTypeId": "1",
        "LoadType": "Flatbed",
        "BillingNotes": "608.48 + 192.60",
        "InvoiceDate": null,
        "PayDate": null,
        "DestinationNext": null,
        "Covered": "0",
        "CompanyName": "GAF Materials Corp.",
        "CarrierName": "Daniel Zamora",
        "FN1": "Craig",
        "FN2": "Craig"
    }
}]

这是输入框

<label for="LoadNumber">Load Number :
    <input type="text" size="12" id="LoadNumber" Name="LoadNumber" tabindex="-1" />
</label>
4

1 回答 1

3

JSON 定义了一个数组,它有一个条目,它是一个对象,它有一个属性,value,它是一个具有一堆属性的对象,其中一个属性LoadNumber。另外,因为LoadNumber是一个input元素,你会使用.value而不是.innerHTML.

所以而不是

document.getElementById("LoadNumber").innerHTML = data.LoadNumber

它应该是:

document.getElementById("LoadNumber").value = data[0].value.LoadNumber;

(分号不是绝对必要的,但我强烈建议不要依赖分号插入的恐怖。)

data指的是整个数组,所以[0]给了我们第一个条目,然后.value给了我们value那个对象的属性,然后给了我们那个.LoadNumber对象的LoadNumber属性。更详细地说:

var firstEntry = data[0];
var valueObject = firstEntry.value;
document.getElementById("LoadNumber").value = valueObject.LoadNumber;

当然,由于您使用的是 jQuery,因此您可以val改用:

$("#LoadNumber").val(data[0].value.LoadNumber);
于 2013-04-09T21:38:54.337 回答