JQuery 自动完成:使用键和标签本地 JS 数组?帮助!
我正在制作一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 This 示例,您应该能够在您的源数据数组并能够在选择事件上访问它们。
因此,您的源数据数组可能如下所示:
然后在您的 select 事件定义中执行以下操作:
因此,区别在于您可以使用 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:
and then in your select event definition do this:
So the difference is that you can then use
ui.item.desc
instead ofui.item.label
like you were using before.创建另一个单独的 js 数组,并将术语作为字符串索引,如您所示。称其为
定义
之类的其他名称,而不是术语
。然后,当您想要附加“内容”div 时,创建一个像这样的新内容,这将调用术语的定义。然后像您刚才那样使用appendChild 函数。
Create another separate js array with the term as the string index, like you show. Call it something else like
definitions
, notterms
. Then when you want to append the "content" div, create a new like thisAnd that will call the terms' definition. Then use the appendChild function like you were just doing.