在 jQuery 中推送到多维数组
我正在尝试使用这个神奇的插件: http://code.drewwilson.com/entry /autosuggest-jquery-plugin
我遇到的问题是,显然我需要用以下内容填充我自己的数据:
kwords.push({"value": "0", "name": item.keyword});
Firebug 报告“kwords”不是一个函数(因为它是不是数组),但我已将示例代码复制并粘贴到插件页面上,如下所示:
var kwords= {items: [
{value: "21", name: "Mick Jagger"},
{value: "43", name: "Johnny Storm"},
{value: "46", name: "Richard Hatch"},
{value: "54", name: "Kelly Slater"},
{value: "55", name: "Rudy Hamilton"},
{value: "79", name: "Michael Jordan"}
]};
所以我的整个代码看起来像(请注意这是一个示例):(
var kwords= {items: [
{value: "21", name: "Mick Jagger"},
{value: "43", name: "Johnny Storm"},
{value: "46", name: "Richard Hatch"},
{value: "54", name: "Kelly Slater"},
{value: "55", name: "Rudy Hamilton"},
{value: "79", name: "Michael Jordan"}
]};
kwords.push({"value": "0", "name": item.keyword});
$("#divSelectedKeywords input").autoSuggest(kwords.items, {selectedItemProp: "name", searchObjProps: "name"});
采用“item.keyword”中的关键字来自 AJAX 调用)。
有人可以解释我做错了什么吗?
I'm trying to use this amazing plugin: http://code.drewwilson.com/entry/autosuggest-jquery-plugin
The problem I'm having is that obviously I need to populate my own data with the following:
kwords.push({"value": "0", "name": item.keyword});
Firebug is reporting that 'kwords' is not a function (because it's not an array), yet I've copied and pasted the example code on the plugin page as follows:
var kwords= {items: [
{value: "21", name: "Mick Jagger"},
{value: "43", name: "Johnny Storm"},
{value: "46", name: "Richard Hatch"},
{value: "54", name: "Kelly Slater"},
{value: "55", name: "Rudy Hamilton"},
{value: "79", name: "Michael Jordan"}
]};
So my entire code looks like (and please note this IS an example):
var kwords= {items: [
{value: "21", name: "Mick Jagger"},
{value: "43", name: "Johnny Storm"},
{value: "46", name: "Richard Hatch"},
{value: "54", name: "Kelly Slater"},
{value: "55", name: "Rudy Hamilton"},
{value: "79", name: "Michael Jordan"}
]};
kwords.push({"value": "0", "name": item.keyword});
$("#divSelectedKeywords input").autoSuggest(kwords.items, {selectedItemProp: "name", searchObjProps: "name"});
(the keyword in the 'item.keyword' is taken from an AJAX call).
Can someone explain what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
kwords
是一个对象,因此不应该有push
方法。我假设您想要做的是
kwords.items.push({"value": "0", "name": item.keyword});
,它的目标是名为的数组
kwords
对象内的项目kwords
is an object, thus there should be nopush
method.What I am assuming you want to do is
kwords.items.push({"value": "0", "name": item.keyword});
, which targets the array nameditems
inside thekwords
object