[英]How do i tell ajax to select the item clicked from a list
Im trying to make a list of people in vacations and I want to calculate their date of return when i click the "Data de Regresso" link.我正在尝试列出休假人员的列表,我想在单击“Data de Regresso”链接时计算他们的返回日期。 I managed to do that but when I click any of the buttons in the list it always passes the first ID in the list and not the ID of the item I clicked.我设法做到了,但是当我单击列表中的任何按钮时,它总是传递列表中的第一个 ID,而不是我单击的项目的 ID。 I'm kinda new to this.我对这个有点陌生。 I would really aprecciate any help.我真的很感激任何帮助。 Im using asp.net framework MVC 5.我使用 asp.net 框架 MVC 5。
Here is the View Code:这是查看代码:
<div class="col-4">
<div class="card">
<div class="card-header text-white" style="background-color: #4d4d4f;">
Férias
</div>
<div class="card-body">
<table class="table">
<thead>
<tr>
<td>Nome</td>
<td></td>
</tr>
</thead>
<tbody>
@if (Model.PessoasDFVM.Count == 0)
{
<tr>
Ninguém de férias.
</tr>
}
else
{
foreach (PortalInternoBBG.Web.Models.HomePage.PessoaDFVM pessoaDeFerias in Model.PessoasDFVM)
{
<tr>
<td>@pessoaDeFerias.Nome</td>
<td>
<div id="ResponseDiv">
<a href="#" onclick="GetDataDeRetorno('@pessoaDeFerias.IDstring')">
Data de Regresso
</a>
</div>
</td>
</tr>
}
}
</tbody>
</table>
</div>
</div>
And the ajax function:和 ajax 函数:
function GetDataDeRetorno(id) {
var stringID = id;
$.ajax({
url: urlCalculaDataDeRetorno,
data: { "id": stringID },
type: "POST",
success: function (data) {
$('#ResponseDiv').replaceWith("<div>" + data + "</div>");
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
}
Append div class with the unique Id so that you will change the div data in ajax response with the help of it.使用唯一的 Id 附加 div 类,以便您可以在它的帮助下更改 ajax 响应中的 div 数据。
Please change the below code in html.请在html中更改以下代码。
<div class="ResponseDiv_@pessoaDeFerias.IDstring">
<a href="#" onclick="GetDataDeRetorno('@pessoaDeFerias.IDstring')">
Data de Regresso
</a>
</div>
and in ajax success with the below code并使用以下代码在 ajax 中成功
success: function (data) {
$('.ResponseDiv_' +stringID).replaceWith("<div>" + data + "</div>");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.