JQuery如何获取元素数据

时间:2021-03-31 17:22:00

标签: jquery asp.net-core

我通过 name = DocumentFile 删除了我的文档 我正在尝试使用 jQuery 获取 href 值和删除:

        <table id="docTable">
            <thead>
            <tr>
                <th scope="col">Document Uploaded</th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            @foreach (var item in @Model.Document)
            {
                <tr>
                    <td>
                        <a asp-route-fileName="@item.DocumentFile">@item.DocumentFile</a>
                    </td>
                    
                    <td>
                        <form>
                            <a class="btn btn-danger" id="DeleteHandleClick" data-toggle="modal" data-target="#delModal">
                                <span class="material-icons">delete_forever</span>
                            </a>
                            <div>
                                <partial name="_DeleteForm"/>
                            </div>
                        </form>
                    </td>
                </tr>
            }
            </tbody>
        </table>
    <script>
    $('#docTable tbody').on( 'click', 'tr','#DeleteHandleClick' , function () {
                    const doc = $(this).closest('tr').attr('documentFile');             
                    document.getElementById('handleDeleteClick').setAttribute("href", "Student/DeleteDocument/" + doc);
                    document.getElementById('modalTitle').innerText ="Are You Sure You Want To Remove This Document?";    
                });
    </script>

我遇到错误 localhost:5001/Article/Details/Student/DeleteDocument/undefined

我使用 getElementId 作为我的部分视图。

我创建了一个表单以使用部分视图确认我的删除。我用一些数据表视图完成了它,但我无法将它应用到我的视图中。

<div class="modal fade" id="delModal" tabindex="-1" role="dialog" aria-labelledby="delModalTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <i class="material-icons mx-auto" style="font-size:80px;color:red">error</i>
        <div id="modalTitle" class="modal-title bg-light text-dark badge bg-primary text-wrap" style="font-size:25px;"></div>
        <div id="modalWarning" class="modal-title badge bg-warning text-dark"></div>
        <div id="modalBody" class="modal-body badge bg-light text-dark text-wrap" style="font-size:15px;"></div>
        <div class="modal-footer">
            <a id="handleDeleteClick" type="button" class="btn btn-primary mx-auto">Yes, Remove it!</a>
            <button type="button" class="btn btn-secondary mx-auto" data-dismiss="modal">Cancel</button>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

应该是这样的:

const doc = $(this).closest('tr').find("td:first a").text();

而且我认为您可以将局部视图放在循环之外

enter image description here