Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.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 在typeahead.js'&引用;“关于选定的”;功能_Javascript_Jquery_Typeahead.js - Fatal编程技术网

Javascript 在typeahead.js'&引用;“关于选定的”;功能

Javascript 在typeahead.js'&引用;“关于选定的”;功能,javascript,jquery,typeahead.js,Javascript,Jquery,Typeahead.js,为标题感到抱歉-这是我第四次尝试 我正在运行一个运行得很好的typeahead.js,但是我想在用户从它创建的下拉列表中选择一个项目时运行一个额外的命令,但它不起作用 $('.typeahead').typeahead( { hint: false, highlight: true, minLength: 3 }, { displayKey: 'name', templates: { suggestion: Handlebars.com

为标题感到抱歉-这是我第四次尝试

我正在运行一个运行得很好的
typeahead.js
,但是我想在用户从它创建的下拉列表中选择一个项目时运行一个额外的命令,但它不起作用

$('.typeahead').typeahead(
{   hint: false,
    highlight: true,
    minLength: 3
},
{
    displayKey: 'name',    
    templates: {
        suggestion: Handlebars.compile("<div><strong>{{name}}</strong> <div class='suggestion'>{{m}} > {{n}}</div></div>")
    },    
    source: list.ttAdapter(),
    limit: 9}).on('typeahead:selected', function (obj, datum) {    
      a  = datum.name;
      b  = datum.m;
      c  = datum.n;

    /* when the user selects an item, this function runs, setting */
    /* variables a, b, and c.  I also want to .addClass and removeClass, */
    /*  but the code doesn't run. */
      $('#search').addClass('fa-unhappy');
      $('#search').removeClass('fa-happy');    
});
还有CSS

.fa-happy {
  color: green;
  opacity: 0.8;
  transition: color 1.5s ease;
}
有趣的是,如果我把这样的东西:

console.log($('#search').width());
在函数中,它返回正确的值,因此我知道它可以找到id为
search
的元素

更新 好的。。。。更奇怪的是,我可以用vanilla js,但不能用jquery:

var d = document.getElementById("search");      
d.className = "fa fa-2x fa-check-square uCheck fa-happy";

这对我来说毫无意义,因为jQuery库是
typeahead.js
工作所必需的(我确信它已加载到页面中)

因此,在您的
typeahead:selected
事件中,是否要设置
#search
使
fa快乐
类?因为那不是你正在做的。您正在添加
fa happy
并删除
fa happy
。也许您只是切换了addClass/removeClass函数?。。。。。。。。。。。。。。。。我是如此的弱智。有时是生活中简单的事情让你……:)没问题,我们都有过这样的时刻!:)。我经常发现,当我完全摸不着头脑的时候,使用JS调试器逐步完成代码行是很有帮助的。你熟悉使用Chrome的内置调试器吗。如果不是的话,我建议阅读这篇文章并练习其中的一些方法。这是非常有用的,可以帮助那些“拔出我的头发”的情况。是的,我一直在使用它,我只是不断地在我的脑海中看它,没有看到我的
。addClass
。removeClass
被切换:)呸。那简直快把我逼疯了!
var d = document.getElementById("search");      
d.className = "fa fa-2x fa-check-square uCheck fa-happy";