每次用户键入时调用函数
Calling a function everytime user types
我有以下函数,每次我都想被调用,用户在typeahead
输入字段中键入一些内容。
function getAllActiveUsers() {
var userList = $('#usersTable').jqGrid('getGridParam').userData;
var userNames = {};
if(userList) {
// Return the list of all active users
$(userList).each(function() {
if(this.userStatus != 1) {
// If the user is verified
// Could be active/inactive
userNames.user = this.username;
}
});
}
return JSON.stringify(userNames);
}
HTML:
<div id="the-basics">
<input class="typeahead" type="text" data-provide="typeahead" placeholder="User List">
</div>
我一直在浏览示例,但不明白如何实现此功能。
编辑:
为什么当我初始化为时它不起作用
$('.typeahead').typeahead({
source : getAllActiveUsers
});
试试这个
$(document).ready(function(){
$('.typeahead').keyup(function(){
getAllActiveUsers();
});
});
您可以使用.keyup jquery函数
$( ".typeahead" ).keyup(function() {
getAllActiveUsers();
});
根据您提供的引用,您可以在id #the-basics
:中指定类.typeahead
$(document).ready(function(){
$('#the-basics .typeahead').typeahead({
//code here;
}
}
由于在文档准备好之前无法安全地操作页面,因此应该使用$(document).ready
此外,请尝试使用浏览器控制台并检查是否可以访问$('#the-basics .typeahead')
您可以使用Jquery Keyup,它在释放键时被触发。
$( ".typeahead" ).on('keyup',function() {
getAllActiveUsers();
});
如果你的文本框是动态的,那么你应该尝试
$(document).on("keyup", ".typeahead" , function() {
getAllActiveUsers();
});
试试这个,让我们知道它是否有效。
这应该是可能的
var getAllActiveUsers = function(q, cb, cb2) {
// cb for sync, cb2 for async
var userList = $('#usersTable').jqGrid('getGridParam').userData;
var filterted = /* whatever you want to do with q */;
cb(filtered);
};
$('.typeahead').typeahead({
/* Options */
},
{
source : getAllActiveUsers
});
相关文章:
- 如何从模块链中调用函数.导出到节点中
- 调用函数内部的函数
- 在javascript中调用c函数
- DropDownListFor赢得't在更改时调用函数
- Javascript页面调用函数
- 在动画结束之前调用函数
- 允许父窗口在其不同域的子iframe上调用函数
- 运行Infinite Scroll后调用函数时出现问题
- JavaScript:在调用函数的文本输入上按enter键
- 使用大括号和不使用bracs调用函数的区别
- javascript在事件上调用函数
- 从index.html调用函数,该函数无限循环
- 从带参数的字符串变量调用函数中的函数
- 为什么 JS 不在滚动时调用函数
- 是否可以在不更改上下文的情况下调用函数.apply
- 如何在ES6类中使用参数调用函数
- AngularJS:调用函数时编号不更新
- JavaScript中的立即调用函数表达式(IIFE)-传递jQuery
- 在JavaScript中调用函数时自定义此选项
- 调用函数中声明的变量