动态表,我可以点击链接并获取上下文信息

时间:2009-08-07 00:37:53

标签: asp.net javascript jquery html

我动态生成了一个表,如下所示:

姓名行动
==== =======
亚当删除
乔删除
删除帐单

我希望“删除”是一个弹出确认弹出窗口的链接:

“你确定要删除”Adam“吗?(是,否)

请注意,“adam”是上下文的,需要从用户选择它的表行中检索。

2 个答案:

答案 0 :(得分:2)

这样的事情:

$(document).ready(function() {
    $('a.delete').click(function(e) {
        //prevent the link from going anywhere
        e.preventDefault(); 
        //give me this link's parent (a <td>) and grab the text of the 'prev' one
        var name = $(this).parent().prev('td').text();
        var answer = confirm("Are you sure you want to delete " + name);
    });
});

假设您为删除链接提供了课程delete

答案 1 :(得分:0)

这应该适用于您提到的每个表结构。

<html>
<head>
<!-- you will need jQuery -->
<script type='text/javascript' src='path/to/jquery.js' ></script>
<script type='text/javascript'>
$(function() {
    $("td[innerHTML*='delete']").click(function(e) {

        var name = $(this).prev('td').text();

        if( confirm("Are you sure you want to delete " + name) )
        {
          // call ajax to delete this record

          // remove tr element
          $(this).parent().remove();
        }
    });
});
</script>
</head>
<body>
    <!-- your table -->
    <table>
      <tr>
        <td>Mira</td>
        <td>delete</td>
      </tr>
        <td>Adam</td>
        <td>delete</td>
      <tr>
        <td>Barney</td>
        <td>delete</td>
      </tr>
      <tr>
        <td>Scott</td>
        <td>delete</td>
      </tr>
    </table>
</body>
</html>