在 jQuery 中推送到多维数组

发布于 2024-11-29 01:38:59 字数 1168 浏览 1 评论 0原文

我正在尝试使用这个神奇的插件: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

成熟的代价 2024-12-06 01:38:59

kwords 是一个对象,因此不应该有 push 方法。

我假设您想要做的是 kwords.items.push({"value": "0", "name": item.keyword});,它的目标是名为 的数组kwords 对象内的项目

kwords is an object, thus there should be no push method.

What I am assuming you want to do is kwords.items.push({"value": "0", "name": item.keyword});, which targets the array named itemsinside the kwords object

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文