0

我有一个复杂的 html 文件。单击表格行时,我想选择表格前面的输入[type=text]。

一个html片段在这里

<input type=text/>
<table>
  <tr>
  ...
  </tr>
</table>

$('tr').click(function() {
  $(this).closest('table').prev(input[type=text]).val();
});

这抛出

`Uncaught TypeError: Object #<HTMLTableRowElement> has no method`closest

单击表格行时如何编写jQuery选择器来选择输入?

4

4 回答 4

2
$(this).parent('table').prev('input[type="text"]').val()

或者

$(table).on('click', 'tr', function () {
    $(this).[prev/siblings/closest]('input[type="text"]').val();
});
于 2013-03-27T12:56:06.280 回答
0

在这里你有一个现场演示:http: //jsfiddle.net/ceETY/2/

你可以使用这个:

$(this).parent('table').prev('input:text').val();
于 2013-03-27T13:00:19.753 回答
0

$(this).parents('table')[0].prev(input[type=text]).val();应该这样做。

于 2013-03-27T12:55:37.190 回答
0

你可以试试 parent()

 $(this).parent('table').prev(input[type=text]).val();
于 2013-03-27T12:56:27.903 回答