JQuery 自动完成:使用键和标签本地 JS 数组?帮助!

发布于 2024-09-30 17:22:00 字数 1030 浏览 4 评论 0原文

我正在制作一个简单的 iPhone 网页,其中列出了大约 200 个医学术语及其定义。我使用 JQuery 自动完成来允许用户动态搜索术语,当他们点击他们想要的术语时,我想在下面显示该术语的定义。我需要帮助设置一个包含术语和定义的 JS 数组。

目前,我有一个这样加载的数组:

var terms = [
        "Abdomen",
        "Ability",
        "Abolish",
etc...

但是我更喜欢的是,如果我知道如何在数组中加载术语及其定义,如下所示:

terms['Abdomen'] = 'stomach, stomach area, belly, tummy';
terms['Ability'] = 'skill';
terms['Abolish'] = 'end, do away with, get rid of';

无论如何,在 terms 数组列表的末尾我这样激活自动完成代码:

$( "#terms" ).autocomplete({ source: terms,});
    $( "#terms" ).autocomplete({ minLength: 2 });
    $( "#terms" ).autocomplete({ delay: 0 });
    $( "#terms" ).autocomplete({ select: function(event, ui) 
    { 
        var myDiv1 = document.getElementById("content");
        myDiv1.appendChild(document.createTextNode(ui.item.label));
    }

现在,当用户点击下拉列表中的术语时,该术语本身(称为 ui.item.label)就会被写入内容 div。我想要的是写下该术语的定义。如何在 JQuery 中进行设置,让自动完成选择功能只需编写定义而不是术语/标签?

I'm making a simple iPhone web page that lists around 200 medical terms and their definitions. I'm using JQuery autocomplete to allow the user to dynamically search the terms, and when they tap the term they want I want to display the term's definition right below. I need help setting up a JS array with both the term and the definition at once.

Currently, I have an array loaded as such :

var terms = [
        "Abdomen",
        "Ability",
        "Abolish",
etc...

What I would prefer however is if I knew how to load a term and its definition in an array like this:

terms['Abdomen'] = 'stomach, stomach area, belly, tummy';
terms['Ability'] = 'skill';
terms['Abolish'] = 'end, do away with, get rid of';

Anyway, at the end of the terms array list I activate the autocomplete code as such:

$( "#terms" ).autocomplete({ source: terms,});
    $( "#terms" ).autocomplete({ minLength: 2 });
    $( "#terms" ).autocomplete({ delay: 0 });
    $( "#terms" ).autocomplete({ select: function(event, ui) 
    { 
        var myDiv1 = document.getElementById("content");
        myDiv1.appendChild(document.createTextNode(ui.item.label));
    }

Right now all this does is when the user taps on the term in the dropdown list, the term itself which is known as ui.item.label just gets written to the content div. What I want is for the term's definition to be written. How do I set this up in JQuery where I can have the autocomplete select function simply write the definition and not the term/label?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

萌化 2024-10-07 17:22:00

根据 This 示例,您应该能够在您的源数据数组并能够在选择事件上访问它们。

因此,您的源数据数组可能如下所示:

var terms = [
    {label : "Abdomen", desc: "stomach, stomach area, belly, tummy"},
    {label : "Ability", desc: "skill"},
    {label : "Abolish", desc: "end, do away with, get rid of"}
etc...

然后在您的 select 事件定义中执行以下操作:

$( "#terms" ).autocomplete({ select: function(event, ui) 
{ 
    var myDiv1 = document.getElementById("content");
    myDiv1.appendChild(document.createTextNode(ui.item.desc));
}

因此,区别在于您可以使用 ui.item.desc 而不是 ui.item。像您之前使用的那样标签。

According to This example you should be able to add all the properties you want in your source data array and be able to access them on the select event.

So your source data array could look like this:

var terms = [
    {label : "Abdomen", desc: "stomach, stomach area, belly, tummy"},
    {label : "Ability", desc: "skill"},
    {label : "Abolish", desc: "end, do away with, get rid of"}
etc...

and then in your select event definition do this:

$( "#terms" ).autocomplete({ select: function(event, ui) 
{ 
    var myDiv1 = document.getElementById("content");
    myDiv1.appendChild(document.createTextNode(ui.item.desc));
}

So the difference is that you can then use ui.item.desc instead of ui.item.label like you were using before.

jJeQQOZ5 2024-10-07 17:22:00

创建另一个单独的 js 数组,并将术语作为字符串索引,如您所示。称其为定义之类的其他名称,而不是术语。然后,当您想要附加“内容”div 时,创建一个像这样的新内容

var newDefinition = definitions[ui.item.label];

,这将调用术语的定义。然后像您刚才那样使用appendChild 函数。

Create another separate js array with the term as the string index, like you show. Call it something else like definitions, not terms. Then when you want to append the "content" div, create a new like this

var newDefinition = definitions[ui.item.label];

And that will call the terms' definition. Then use the appendChild function like you were just doing.

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