如何获取在数据表中搜索框中输入的值 [英] How to get value entered in search box In Datatables
问题描述
如何获取在数据表中搜索框中输入的值。
有没有办法在数据表中的搜索框中输入值?
How to get value entered in search box In Datatables. Is there a way to get value entered in search box in Data Tables?
推荐答案
如果您只是想检查执行搜索时的值[dataTables 1.10.x ]:
If you just want to check the value when a search is performed [dataTables 1.10.x] :
var table = $('#example').DataTable();
$('#example').on('search.dt', function() {
var value = $('.dataTables_filter input').val();
console.log(value); // <-- the value
});
如果要在搜索前检查值,并且可以取消搜索,则必须取消绑定默认搜索框事件并创建自己的,如下所示:只有当用户输入超过3个字符时才搜索:
if you want to check the value before the search, and be able to cancel the search, you must unbind the default searchbox event and create your own, like this - search only when the user has entered more than 3 characters :
$('.dataTables_filter input').unbind().keyup(function() {
var value = $(this).val();
if (value.length>3) {
table.search(value).draw();
}
});
demo - > http://jsfiddle.net/pb0632c3/
demo -> http://jsfiddle.net/pb0632c3/
要完全重置搜索/过滤器,就像用户已经删除搜索项:
To reset the search / filter completely, like if the user has deleted the search term :
if (value.length==0) table.search('').draw();
这篇关于如何获取在数据表中搜索框中输入的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!