Jquery遍历表(错误:不是函数)

时间:2013-06-12 13:54:07

标签: jquery dom-traversal

$(function(){
  $('.swaction').click(function(event){
    var num = $(this).parent().parent().children().first().children.first().val();
    alert(num);
    event.preventDefault();
  });
})

<table class="table-a" id="unrecognizedPDF">
  <thead>
    <tr>
      <th>Nombre de archivo</th>
      <th>Fecha de creación</th>
      <th>Acciones</th>
     </tr>
   </thead>
<tbody>
  <tr>
   <td>12313242122333</td>
   <td>07/06/2013 10:52:52</td>
   <td><a href="#" data-action="ver" class="swaction">Ver</a> |
       <a href="#" data-action="renombrar" class="swaction">Renombrar</a> |
       <a href="#" data-action="ver" class="swaction">Eliminar</a>
   </td>
  </tr>
 </tbody>
 </table>

我想得到这个号码:12313242122333

我试过这个:var num =

  

$(本).parent()的父()的儿童()第一()children.first()VAL();。。。

但是Firebug说这不是一个功能。什么是错的任何线索?这是我第一次尝试遍历树。

谢谢!

错误:

TypeError: $(...).parent(...).parent(...).children(...).first(...).children.first is not a function
http://192.168.100.196/printbox/js/js_procesar_escaneos.js
Line 3

4 个答案:

答案 0 :(得分:2)

改为使用var num = $(this).closest('tr').find('td:first').html();

<强> jsFiddle example

.val()用于输入元素,您希望获取您使用.html()的HTML。

答案 1 :(得分:1)

你不能在td元素上使用.val(),它是表单元素。尝试.text()

$(this).parent().parent().children().first().text();

答案 2 :(得分:1)

children.first()

您尝试调用first() 功能children方法。

您需要使用括号调用 children函数。

答案 3 :(得分:1)

$(this).closest('tr').find('td:first').html();

试试这个。

使用html代替val

相关问题