【发布时间】:2018-09-18 00:47:47
【问题描述】:
我有这个过滤表的脚本: (把这个脚本改成函数)
$(document).ready(function(){
var $rows = $('tbody > tr'),
$filters = $('#tableP input');
$filters.on("keyup", function () {
var $i = $filters.filter(function () {
return $.trim(this.value).length > 0;
}),
len = $i.length;
if (len === 0) return $rows.show();
var cls = '.' + $i.map(function () {
return this.className
}).get().join(',.');
$rows.hide().filter(function () {
return $('td', this).filter(cls).filter(function() {
var content = this.textContent.toLowerCase(),
inputVal = $i.filter('.' + this.className).val().toLowerCase();
return content.indexOf(inputVal) > -1;
}).length === len;
}).show();
});
});
<script src="http://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript"></script>
<table id='tableP'>
<thead>
<tr>
<th>
<input type='text' class="nome" placeholder="Nome.."/>
</th>
<th>
<input type='text' class="cognome" placeholder="Cognome.." />
</th>
<th>
<input type='text' class="citta" placeholder="Città.." />
</th>
</tr>
</thead>
<tbody>
<tr>
<td class='nome'>Carlo</td>
<td class='cognome'>Grasso</td>
<td class='citta'>Italia</td>
</tr>
<tr>
<td class='nome'>Giuseppe</td>
<td class='cognome'>Puglisi</td>
<td class='citta'>Italia</td>
</tr>
<tr>
<td class='nome'>Franc</td>
<td class='cognome'>Justin</td>
<td class='citta'>Francia</td>
</tr>
</tbody>
</table>
这行得通! 我想创建一个函数来调用输入标签,我不知道该怎么做。我想创建一个函数来调用输入标签,我不知道怎么做。
我想在标签输入 onkeyup myFilter() 中创建函数,我该怎么做?
有人能帮我吗?
【问题讨论】:
-
不明白你的目标是什么...“调用输入标签的函数”是什么意思。请以更清晰的方式描述您想要的结果/目标
-
我想把这个脚本变成一个可以从输入标签调用的函数,这样我就可以通过调用表名来在许多页面上使用这个函数,例如。 myFilter (IDnameTable)
-
我想要:
-
我正在学习jquery,我是初学者。
标签: jquery filter html-table