【问题标题】:dynamic table where i can click on link and grab contextual information我可以单击链接并获取上下文信息的动态表
【发布时间】:2009-08-07 00:37:52
【问题描述】:

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

命名动作
==== =======
亚当删除
乔删除
账单删除

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

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

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

【问题讨论】:

    标签: asp.net javascript jquery html


    【解决方案1】:

    类似这样的:

    $(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 类。

    【讨论】:

    • 在 $(document).ready() 中,在脚本标签内,在文档的 HEAD 中。
    • 很抱歉让我很痛苦,但我还是迷路了。我把它放在最上面,但是在哪里将链接与此功能相关联?
    • 是“.delete”我必须在链接周围提供一些 div 的 id?
    • .delete 是您要提供删除链接的类,例如
    • @me - 如果您需要更多帮助,请告诉我,这不麻烦。
    【解决方案2】:

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

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 2018-12-23
      相关资源
      最近更新 更多