代码之家  ›  专栏  ›  技术社区  ›  Adi Sembiring

使用jquery获取行数据

  •  2
  • Adi Sembiring  · 技术社区  · 15 年前

    我实现了这样的事情

    - person grid -
    name        address     action
    a           ax          edit
    b           bx          edit
    
    - form edit -
    id         :
    name       :
    address    :
    <submit edit>
    

    动作编辑是一个链接,假设锚标记是: <a href="http://localhost/person/edit/1"> 当我点击编辑链接时。人员信息将显示在id、name、address文本文件中。 假设,输入的文件名。 txt_id, txt_name, txt_address .

    我的实现是:

    $("a").click(function(event) {
        $.parrent = $(this).parent().parent();
        $.id = ... //get id
        $.name = ...//get name
        $.address = ...//get address
    
        ${"txt_id"}.val($.id);
        ${"txt_address"}.val($.address);
        ${"txt_name"}.val($.name);
        return false;
    });
    

    我不知道如何实现获取ID,获取名称,获取地址后,我得到的鹦鹉。 有什么建议吗?或者还有其他解决办法吗?

    Edit
    

    在单击SubmitEdit时,我将显示当前编辑行的新名称和地址。

    2 回复  |  直到 15 年前
        1
  •  3
  •   Doug Neiner    15 年前

    这应该符合您的要求:

    $("a").click(function(e) {
        var $td      = $(this).closest('tr').children('td'),
            $id      = parseInt($(this).attr('href').replace('http://localhost/person/edit/',''), 10),
            $name    = $td.eq(0).text(),
            $address = $td.eq(1).text();
    
        $("#txt_id").val($id);
        $("#txt_address").val($address);
        $("#txt_name").val($name);
    
        e.preventDefault();
    });
    
        2
  •  0
  •   Community CDub    8 年前

    使用jquery td:nth child(n)从每个td单元格中获取值。看到类似的问题 How to get a table cell value using jquery?