填充<表>行

Populate <Table> rows with HTML

本文关键字:gt 填充 lt      更新时间:2023-09-26

我在屏幕上有一个空白表:

<table id="TransactionTable" class="table table-responsive table-striped">
    <thead>
        <tr>
            <th></th>
            <th>Date</th>
            <th>Account</th>
            <th>Third Party</th>
            <th class="text-right">Amount</th>
        </tr>
    </thead>
    <tbody></tbody>
</table>

在我的控制器中,我为这个表建立了一些HTML行,使用文本:

foreach(var i in transactions) {
    tblCode += "<tr>";
    tblCode += string.Format(
        "<td><a href='"@Url.Action('"EditTransaction'", '"Transaction'" class='"btn btn-sm btn-success'" title='"View Transaction'">View</a></td>");
    tblCode += string.Format("<td class='"small-bold-cell'">{0}</td>", i.Date.ToString("dd.MMM.yyyy"));
    tblCode += string.Format("<td class='"small-cell'">{0}</td>", i.EntityEventTypeId == (int) Constants.EntityEventTypes.Payment ? i.DestinationEntityFull : i.SourceEntityFull);
    tblCode += string.Format("<td class='"small-cell'">{0}</td>", i.EntityEventTypeId == (int) Constants.EntityEventTypes.Payment ? i.SourceEntityFull : i.DestinationEntityFull);
    tblCode += string.Format("<td class='"small-bold-cell text-right'">{0}</td>", i.Amount.ToString("C2"));
    tblCode += "</tr>";
}

然后,我在JSON结果中将其传递回我的javascript,使用Chrome,我可以在返回的对象中看到HTML代码。

然后我尝试将HTML代码添加到我的表中。

if (result.Success == 'true') {
    $.unblockUI();
    $('#TransactionTable').innerHTML = result.TableCode;
    $('#transactions').modal('show');
}
else {
    $.unblockUI();
    alert("error");
}

但它似乎根本没有添加代码。一旦显示,表中就没有任何内容。

我做错了什么?

jQuery对象没有innerHTML属性-您需要使用html方法,或者引用jQ对象的0索引来获得原生DOM对象:
$('#TransactionTable')[0].innerHTML = result.TableCode;

$('#TransactionTable').html(result.TableCode);