Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 每次用户键入时调用函数_Javascript_Jquery_Typeahead.js_Typeahead_Bootstrap Typeahead - Fatal编程技术网

Javascript 每次用户键入时调用函数

Javascript 每次用户键入时调用函数,javascript,jquery,typeahead.js,typeahead,bootstrap-typeahead,Javascript,Jquery,Typeahead.js,Typeahead,Bootstrap Typeahead,我有下面的函数,我希望每次都被调用,用户在typeahead输入字段中键入一些内容 function getAllActiveUsers() { var userList = $('#usersTable').jqGrid('getGridParam').userData; var userNames = {}; if(userList) { // Return the list of all active users $(userLis

我有下面的函数,我希望每次都被调用,用户在
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:


您可以使用.keyup jquery函数

$( ".typeahead" ).keyup(function() {
  getAllActiveUsers();
});

您可以使用.keyup jquery函数

$( ".typeahead" ).keyup(function() {
  getAllActiveUsers();
});
试试这个

$(document).ready(function(){
   $('.typeahead').keyup(function(){
       getAllActiveUsers();
   });
});
试试这个

$(document).ready(function(){
   $('.typeahead').keyup(function(){
       getAllActiveUsers();
   });
});

根据您提供的参考资料,您可以在id
中指定类
.typeahead
。#basics

$(document).ready(function(){
  $('#the-basics .typeahead').typeahead({
    //code here;
  }
}
由于在文档准备就绪之前无法安全操作页面,因此您应该使用
$(document.ready


此外,请尝试使用浏览器控制台,检查是否可以访问
$(“#基础知识.typeahead”)

从您提供的参考中获取。您可以在id
中指定类
.typeahead

$(document).ready(function(){
  $('#the-basics .typeahead').typeahead({
    //code here;
  }
}
由于在文档准备就绪之前无法安全操作页面,因此您应该使用
$(document.ready


另外,尝试使用浏览器控制台并检查是否可以访问
$(“#basics.typeahead”)

您可以使用Jquery-Keyup,当释放键时会触发它

$( ".typeahead" ).on('keyup',function() {
  getAllActiveUsers();
});

您可以使用jquerykeyup,它在释放密钥时被触发

$( ".typeahead" ).on('keyup',function() {
  getAllActiveUsers();
});

如果你的文本框是动态出现的,那么你应该试试

$(document).on("keyup", ".typeahead" , 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
});
这应该是可能的

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
});

调用
$('.typeahead')上的函数。在('keyup',getAllActiveUsers())
尝试此操作您使用的是哪种类型的typeahead?您指的是typehead.js,但有人告诉我您使用的是另一个?
getAllActiveUsers
返回什么?在
$('.typeahead')上调用函数。在('keyup',getAllActiveUsers())
上尝试一下您使用的是哪种类型的typeahead?您指的是typehead.js,但有人告诉我您使用的是另一个?getAllActiveUsers
返回什么?为什么使用匿名函数?那是多余的,你说得对。匿名函数可以被需要执行的函数替换。但它更容易阅读,不起作用。我正在尝试使用twitter的typeahead。我没有使用该插件,但我想你提供的数据格式不对。为什么要使用匿名函数?那是多余的,你说得对。匿名函数可以被需要执行的函数替换。但它更容易阅读,不起作用。我正在尝试使用twitter的typeahead。我没有使用该插件,但我想你提供的数据格式不对。为什么要使用匿名函数?这就是冗余。为什么要使用匿名函数?那是多余的。