【发布时间】:2014-06-11 21:15:33
【问题描述】:
我正在使用这个 jQuery Autocomplete,总的来说它工作得很好: https://github.com/devbridge/jQuery-Autocomplete
虽然用户从选项中进行选择,但我正在努力处理回调函数:
<input type="text" class="form-control vrm" id="type-autocomplete" />
<script type="text/javascript">
// Create autocomplete:
$('#type-autocomplete').autocomplete({
serviceUrl:'autocomplete.php?searchtype=product',
noCache: true,
delimiter: /(,|;)\s*/, // regex or character
onSelect: function(value, data){ alert('You selected: ' + value + ', ' + data); },
});
</script>
我收到的警报是:
你选择了:[object Object],未定义
我真的需要使用“数据”部分来填充一些隐藏的表单字段。请问有什么想法吗?
编辑:
我已经按照这里的集成指南进行了操作,这似乎与 github 过时了: http://www.devbridge.com/sourcery/components/jquery-autocomplete/
这段代码现在可以工作了:
onSelect: function(suggestion){ alert('You selected: ' + suggestion.value + ', ' + suggestion.data); },
【问题讨论】:
-
Read the documentation,它说明了你应该做什么的一个例子。此外,
console.log(value)应该在调试控制台中显示该对象,而不是将其转换为字符串。 -
啊,看看我的编辑。我应该从一开始就关注 github,这是我刚刚添加到我的 EDIT 中的原始 URL。感谢您的提醒。
标签: javascript jquery ajax autocomplete