简体   繁体   English

TypeAhead.js突出显示TypeError

[英]TypeAhead.js Highlight TypeError

I'm trying to get the selected value of a typeahead input and I get this error every time: Uncaught TypeError: Cannot assign to read only property 'highlight' of . 我正在尝试获取typeahead输入的选定值,每次都会出现此错误: Uncaught TypeError: Cannot assign to read only property 'highlight' of It never tells me what object it's referring to and it originates from within the TypeAhead.min.js file, so I can't say exactly what line of my code is causing it. 它永远不会告诉我它指的是什么对象,它来自TypeAhead.min.js文件,所以我不能确切地说我的代码是什么行引起它。 Here's the code I use to set up and capture the value of the input: 这是我用来设置和捕获输入值的代码:

$(".articles.new").ready ->
  engine = new Bloodhound {
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  local: autocomplete_items
}

engine.initialize()

$('#auto_complete').typeahead { hint: true, highlight: true, minLength: 1 },  
  { name: "names", displayKey: "name", source: engine.ttAdapter()}

$('#auto_complete').on 'typeahead:selected', (event, selection) ->
  alert selection.name
  $('#auto_complete').typeahead 'setQuery', ''
  event.stopPropagation()
  return
return

For context: auto_complete items is an array of objects from my controller with name property. 对于上下文:auto_complete项是来自我的控制器的具有name属性的对象数组。 I'm working in Ruby on Rails 4.1.4. 我在Ruby on Rails 4.1.4上工作。

What can I do to fix this error? 我该怎么做才能解决这个错误? What is causing it? 是什么造成的?

I figured this out after much experimenting. 经过多次试验,我想出了这个。 The error was coming from this line $('#auto_complete').typeahead 'setQuery', '' . 错误来自这一行$('#auto_complete').typeahead 'setQuery', '' I was trying to clear the value from the input box after taking some other action with it. 在尝试使用其他操作后,我试图清除输入框中的值。 I'm not too sure of the technical details, but using $('#auto_complete').typeahead 'val', '' instead achieved the desired effect without the error. 我不太清楚技术细节,但使用$('#auto_complete').typeahead 'val', ''取而代之的是没有错误的预期效果。 I suppose that explains why the object was blank, it was referring to an empty query and trying to take actions with it. 我想这解释了为什么对象是空白的,它指的是一个空查询并尝试用它来执行操作。

If anybody can give me a more detailed explanation of the difference between the two API calls I'd be interested to hear it out of curiosity, but if not I'm just happy to move on. 如果有人可以给我一个更详细的解释,我会有兴趣听到两个API调用之间的差异,但如果没有,我很乐意继续前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM