我需要捕获在表格上单击的任何项目并将其放入隐藏输入中。 据我所知,这是:
var $rows = $('#table tbody tr');
$('#search').keyup(function() {
var val = '^(?=.*\\b' + $.trim($(this).val()).split(/\s+/).join('\\b)(?=.*\\b') + ').*$',
reg = RegExp(val, 'i'),
text;
$rows.show().filter(function() {
text = $(this).text().replace(/\s+/g, ' ');
return !reg.test(text);
}).hide();
});
$('td').click(function() {
var txt = $('td:first-child').text();
$('#tipo').val(txt);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input-group">
<span class="input-group-addon" id="rotulo">Filtrar:</span>
<input type="text" class="form-control" placeholder="" aria-describedby="rotulo" id="search">
</div>
<table class="table" id="table">
<tr>
<td>1st Value</td>
</tr>
<tr>
<td>2nd Value </td>
</tr>
<tr>
<td>3rd Value </td>
</tr>
</table>
<input type="text" id="tipo" value="">
问题是,无论我单击什么值,表中的许多项目都会被选中,即使第一个项目被隐藏。 正在使用Jquery。
最佳答案
td:first-child
选择第一个子 td,这就是为什么您总是获得第一个元素。
可以使用$(this).text()检索被点击的元素文本
关于javascript - 无论单击表中的哪个项目,都将该值放在隐藏输入上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35514003/