简体   繁体   English

我如何告诉 ajax 选择从列表中单击的项目

[英]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.

相关问题 单击列表时如何使用列表中项目的信息来渲染模态 - How do I render a modal with information from an item in the list when clicked on it 如何在使用JavaScript的会话中存储单击列表项? - How do I store a clicked list item in a session with JavaScript? 如何使用 item.description 在列表视图中选择一个项目 - how do I select an Item in list view with item.description 当使用Jquery / JavaScript单击上一个列表项时,如何定位嵌套在列表项内的元素? - How do I target an element nested inside a list item when the previous list item is clicked with Jquery/JavaScript? 填充后如何从选择列表中选择项目? - How can I select an item from a select list after populating it? 如何在同一单击事件 Jquery 的无序列表中获取先前单击的列表项 - How do I get the previously clicked list item in an unordered list in same click event Jquery 在Ember中单击某个项目时,如何将活动类添加到项目列表中? - How do I add an active class to a list of items when an item is clicked on in Ember? 如何实现单击Html菜单中的特定列表项时所采取的操作? - How do I implement the action taken when a specific list item in an Html menu is clicked? 如何使用jQuery选择CLICKED项目? - How to select the CLICKED item with jQuery? 如何判断是否在javascript中单击了列表元素? - How to tell if an element of a list was clicked in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM