jquery.autocomplete.js - 自动完成工作如何? [英] jquery.autocomplete.js - how does autocomplete work?
问题描述
我用GET请求的结果调用自动完成jQuery。
自动完成功能调用如下所示:
I call the autocomplete jquery with the result of a GET request. The autocomplete function call looks like this:
$('#id_project_owner_externally').autocomplete('/pm/contact_autocomplete');
url / pm / contact_autocomplete返回一个元组列表。元组的第一部分是联系人的名称,元组的第二部分是联系人的ID。
The url /pm/contact_autocomplete returns a list of tuples. The first part of the tuple is the name of the contact and the second part of the tuple is the id of the contact.
相应的功能(django视图的一部分)看起来像这样:
The corresponding function (part of a django view) looks like this:
def iter_results(results):
if results:
for r in results:
yield '%s|%s\n' % (r.first_name, r.id)
现在我想知道jquery autocomplete是用first_name + id元组做的。 Acutally将first_name放入输入字段。但是id部分会发生什么。这是我需要的重要信息。
Now I'm wondering what jquery autocomplete is doing with the first_name + id tuple. Acutally the first_name is put into the input field. But what happens with the id part. This is the important information that I need.
我可以告诉jquery该id应该放在某个隐藏的字段中吗?
Can I tell jquery that the id should be placed into a certain hidden field?
编辑:解决方案
<script type="text/javascript"><!--//
$('#id_project_manager_externally').autocomplete('/pm/contact_autocomplete').result(function(event, item) {$('#id_project_manager_externally_hidden').attr("value", item[1]);});//--></script>
推荐答案
插件文档有这个例子:
var data = [ {text:'Link A', url:'/page1'}, {text:'Link B', url: '/page2'} ];
$("...").autocomplete(data, {
formatItem: function(item) {
return item.text;
}
}).result(function(event, item) {
location.href = item.url;
});
所以你基本上可以有一个.result()选项来填充一个隐藏的字段。
例如 $('#my_hidden_field')。val(item.extra_value);
So you basically can have a .result() option that you use to populate a hidden field.
e.g. $('#my_hidden_field').val(item.extra_value);
这篇关于jquery.autocomplete.js - 自动完成工作如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!