我有一个表,其中我绑定了来自表单的值。在该表单中,我有一个主键字段作为 TicketId,我将其隐藏在表单中,并在将其插入表时显示它。对于绑定数据,我使用了 Knockout。所以我想删除我将选择的行。所以在选择它时我应该获取该行的 id,以便我可以使用 ajax 将其传递给 Delete 操作。但我的问题是我没有得到那个id。那么怎么做呢?我的代码:
<table id="table2" style="border: double">
<thead>
<tr>
<td>Ticket ID</td>
<td>Ticket Type</td>
<td>No of Tickets</td>
<td>Ticket Price</td>
<td>Start Date</td>
<td>End Date</td>
<td>Action</td>
</tr>
</thead>
<!--Iterate through an observableArray using foreach-->
<tbody id="ticketid" data-bind="foreach:TicketDatas">
<tr style="border: solid" data-bind="click: $root.getselectedTicket" id="updtr">
<td id="rowid" data-bind="text:TicketId">@*<span data-bind="text:No_Of_Ticket"></span>*@</td>
<td data-bind="text:SelectedTicketType">@*<span data-bind="text:No_Of_Ticket"></span>*@</td>
<td data-bind="text:No_Of_Ticket">@*<span data-bind="text:No_Of_Ticket"></span>*@</td>
<td data-bind="text:Ticket_Price">@*<span data-bind="text:Ticket_Price"></span>*@</td>
<td data-bind="text:Start_Date">@*<span data-bind="text:Start_Date"></span>*@</td>
<td data-bind="text:End_Date">@*<span data-bind="text:End_Date"></span>*@</td>
<td>
<button data-bind="click: $root.deleterec">Delete</button></td>
</tr>
</tbody>
</table>
<script type="text/javasript">
self.deleterec = function () {
if (confirm('Are you sure to Delete this ticket ??')) {
var tickid = $("#table2 tr:eq(0)").attr("id");
$.ajax({
type: "POST",
data: { id: tickid },
url: "Ticket/DeleteTicket",
//data: "{id:" + ko.toJSON(id) + "}",
success: function (data) {
self.TicketDatas.remove(data);
alert("Record Deleted Successfully");
//GetTickets();//Refresh the Table
},
error: function (error) {
alert(error.status + "<--and--> " + error.statusText);
}
});
}
};
</script>
所以如果我简而言之,只想要这个陈述的解决方案
var tickid = $("#table2 tr:eq(0)").attr("id");