如何获取在数据表中的搜索框中输入的值 [英] 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();
}
});
演示 -> 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屋!