在 javascript 中将项目推送到数组中

Pushing an item into array in javascript

本文关键字:数组 项目 javascript      更新时间:2023-09-26

我遇到了关于JavaScript的问题。具体来说,我有一个像

<table class="table">
    <tr>
        <th>
            <label>Select</label>
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Middle)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DoB)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Gender)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StartDate)
        </th>
        <th></th>
    </tr>
@foreach (var item in Model) {
    <tr id="@Html.DisplayFor(modelItem => item.EmployeeId)" ondblclick="DoubleClickOnRow(this)">
        <td>
            <input id="@Html.DisplayFor(modelItem => item.EmployeeId)" type="checkbox" onclick="SelectCheckBox(this)"/>
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Middle)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DoB)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Gender)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.StartDate)
        </td>
    </tr>
}
</table>

我声明了一个数组来保存 EmployeeId 从选中行第一列中的复选框。但是,它说arrayOfEmployeeId.push(id);而不是一个函数。顺便说一下,我想获取 EmployeeId 数组以 ASP.NET web API 中执行 DELETE 方法。我说的对吗?

var arrayOfEmployeeId = {};
function SelectCheckBox(thisRow)
    {
        var id = thisRow.id;
        arrayOfEmployeeId.push(id);
        console.log(arrayOfEmployeeId);
    }

arrayOfEmployeeId是一个对象({})。您可能想要的是像 var arrayOfEmployeeId = []; 这样的数组。

arrayOfEmployeeId = {}; 是一个对象你可以把它变成一个数组。

arrayOfEmployeeId = [];您可以使用拼接方法根据需要删除。