我有一個表格,其中我綁定了來自表單的值。在那個表單中,我有一個主鍵字段作爲TicketId,我一直隱藏在表單中,並將它插入到表格中, it.For綁定數據我已經使用Knockout.So我想要刪除我將選擇的行。因此,選擇它時,我應該得到該行的id,以便我可以將它傳遞給刪除操作使用ajax.But我問題是我沒有得到那個id.So該怎麼做? 我的代碼:如何使用JQuery獲取表的第一個td的值?
<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");
嘗試這個$(「#table2 tr:first」)。attr(「id」); –
不應該跳過頭部的TR? $(「#table2 tbody tr:first」)。attr(「id」); – Jasper