简体   繁体   English

jQuery:在表中选择一行并使用其数据?

[英]Jquery: Select a row in a Table and use its data?

I have a table where i want to be to both select a row and delete a row, but i can't figur out how to do this with JQuery. 我有一个表,我想同时选择一行和删除一行,但是我无法弄清楚如何使用JQuery做到这一点。 The code i have only effects the first row in the table. 我的代码仅影响表格的第一行。 So i can remove the first row but no other. 因此,我可以删除第一行,但不能删除其他行。 What is wrong. 怎么了。 Here is what i tried: 这是我尝试过的:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript" language="javascript">
    $(document).ready(function() {
        $("#newCandidatesTable").tablesorter();
        var candidateID = $("#candidateID").text();

        // Event actions
        $("#acceptCandidateButton_" + candidateID).click(function(e) {
            $.post("/Admin/AcceptCandidate/", { candidateID: $("#candidateID").text() }, completeAccept());
        });

        // Functions
        function completeAccept() {
            showMsgBox($("#candidateName").text() + " er blevet accepteret, som ansøger til stillingen: ...");
            $("#tr_" + candidateID).remove();
        }

        function getFirstNameFromFullName(fullName) {
            var nameArray = new Array();

            nameArray = fullName.toString().split(" ");

            return nameArray[0];
        }
    });
</script>
<h2>Nye ansøgere</h2>
<table id="newCandidatesTable">
    <thead>
        <tr>
            <th style="cursor: pointer;">ID</th>
            <th style="cursor: pointer;">Navn</th>
            <th style="cursor: pointer;">Email</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
<% foreach (var candidate in Model)
{
     %>
        <tr id="<%= "tr_" + candidate.iAnsogerID %>">
            <td><div id="candidateID"><%= candidate.iAnsogerID %></div></td>
            <td><div id="candidateName"><%= candidate.vNavn %></div></td>
            <td><div id="candidateEmail"><%= candidate.vEmail %></div></td>
            <td>
                <div id="<%= "acceptCandidateButton_" + candidate.iAnsogerID %>" style="cursor: pointer; border: 1px solid black; width: 150px; text-align: center;">Godkend</div>
            </td>
        </tr>
     <%
} %>
</tbody>
</table>

I think you need to wrap your event hookup in a foreach to iterate over the collection of items you have. 我认为您需要将事件连接封装在foreach中,以迭代所拥有的项的集合。

$("#candidateID").each(function() {
  // Event actions
  var candID = this.text;
  $("#acceptCandidateButton_" + candID).click(function() {
    $.post("/Admin/AcceptCandidate/", { candidateID: candID }, completeAccept());
  });
});

I've not had the chance to test this but if nothing else it should get you on the right track. 我还没有机会进行测试,但是如果没有别的选择,它应该会让您走上正确的道路。

I guess you could also do this directly in your page generation loop, ie: 我想您也可以在页面生成循环中直接执行此操作,即:

<td>
  <div id="<%= "acceptCandidateButton_" + candidate.iAnsogerID %>" style="cursor: pointer; border: 1px solid black; width: 150px; text-align: center;" onclick="$.post("/Admin/AcceptCandidate/", { candidateID: " + candidate.iAnsogerID + " }, completeAccept())">Godkend</div>
</td>

You're creating <div id="candidateID"> (along with the two other div s that go with it) inside your loop, so you're reusing that id="candidateID" attribute for every candidate you have, which is sure to break things. 你创建<div id="candidateID">与其他两个一起div你的循环 ,随着它走S),所以你重用你有每名候选人,这是肯定的id =“candidateID”属性打破事物。 In this case, jQuery (and the underlying browser, of course) is choosing to recognize only the first such field, and thus you can only ever delete the first row. 在这种情况下,jQuery(当然还有底层的浏览器)选择仅识别此类字段的第一个,因此,您只能删除第一行。

The line of code that does the actual removal is correct: 实际删除的代码行是正确的:

$("#tr_" + candidateID).remove();

You just need to make sure that candidateID is set to something that makes sense. 您只需要确保将candidateID设置为有意义的值即可。 Without more context, I'm not sure what that would be. 没有更多的上下文,我不确定会是什么。

You don't actually need to interpolate anything into the selector for deleting the TR, for example, this will work: 您实际上不需要在选择器中插入任何内容来删除TR,例如,这将起作用:

    $("#acceptCandidateButton_" + candidateID).click(function(e) {
        $.post("/Admin/AcceptCandidate/", { candidateID: $("#candidateID").text() }, completeAccept($(this));
    });

    // Functions
    function completeAccept(context) {
        showMsgBox($("#candidateName").text() + " er blevet accepteret, som ansøger til stillingen: ...");
        context.parent().parent().remove();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM