将 jquery 自动完成动态文本框绑定到 json
我已经进行了大量搜索,但似乎一无所获。
我有一些 json 看起来像
[{"IngredientId":1,"IngredientName":"Butter","CategoryID":1},{"IngredientId":2,"IngredientName":"Sugar","CategoryID":1},{"IngredientId":3,"IngredientName":"Water","CategoryID":1},{"IngredientId":4,"IngredientName":"Salt","CategoryID":1}]
我想做两件事。 1. 以某种方式存储所选自动完成文本框中的 IngredientId 和最重要的内容。将自动完成绑定到上面的 json。
这是我绑定值的蹩脚尝试
.autocomplete({
source: ingredients,
select: function (item) {
console.log(item.IngredientId);
return item.IngredientName;
}
有人可以帮助我以正确的方式绑定到自动完成吗?
谢谢
I have done a heap of searching but can't seem to get anywhere.
I have some json that looks like
[{"IngredientId":1,"IngredientName":"Butter","CategoryID":1},{"IngredientId":2,"IngredientName":"Sugar","CategoryID":1},{"IngredientId":3,"IngredientName":"Water","CategoryID":1},{"IngredientId":4,"IngredientName":"Salt","CategoryID":1}]
Im trying to do 2 things. 1. Somehow store IngredientId from the selected autocomplete textbox and the most important.Bind the autocomplete to the above json.
This is my lame attempt of binding the value
.autocomplete({
source: ingredients,
select: function (item) {
console.log(item.IngredientId);
return item.IngredientName;
}
Can someone help out with the right way I should be binding to the autocomplete?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
source
属性需要纯数组或对象数组(或字符串)。对于对象数组,这些对象的类型应该是{ 'value': 'some_val', 'label': 'some_label' }
。如果您无法控制该成分数组,您可能应该映射它以便使用自动完成功能。
然后使用带有自动完成功能的新数组
source
property requires plain array or array of objects (or string). Going with array of objects those should be of the type{ 'value': 'some_val', 'label': 'some_label' }
.If you don't have control over that ingredients array you should probably map it in order to work with autocomplete.
then use the new array with autocomplete