如何从 jQuery Autocomplete 控件检索选定的值
我的代码是:
<script type="text/javascript">
var ab;
$(function() {
$.getJSON('jsonSample.action',null,function(json) {
ab = json.languageList;
$("#tags").autocomplete({
data:ab
});
});
});
</script>
此代码工作正常,当我开始在已实现此功能的文本框中输入内容时,显示所有建议,现在的问题是一旦选择一个值,我想触发另一个事件来自建议列表...还有一个问题是当用户从自动完成列表中选择时如何获取所选值..
我参考了 stackoverflow 中的教程 此处
$(".tags").change(function(me) {
alert(this.valu);
});
此代码显示我在文本框中输入的值,它不显示我在文本框中输入的值在自动完成中选择..
我使用了另一种方法;
$(".tags").result(function(event, data, formatted) {
var u = this;
// Check value here
});
当我用 firebug 检查它时,它说找不到方法 ....我想显示从自动填充列表中选择的项目,并且在我选择一个项目后它必须触发另一个事件。 。
提前致谢..
My code is :
<script type="text/javascript">
var ab;
$(function() {
$.getJSON('jsonSample.action',null,function(json) {
ab = json.languageList;
$("#tags").autocomplete({
data:ab
});
});
});
</script>
This code works fine, when i start type in textbox where i have implemented this, displays all suggestion, now the problem is i want to trigger a another event as soon as one value is selected from suggestion list... And also another question is How to get the selected value as soon as the user select from autocomplete list..
I referred the tutorial which is in stackoverflow here
$(".tags").change(function(me) {
alert(this.valu);
});
This code displays the value what i typed in textbox, it is not displaying what the item i select in autocomplete..
I used another method ;
$(".tags").result(function(event, data, formatted) {
var u = this;
// Check value here
});
When i check it with firebug , it says there no method found .... I want to show the item selected from auto populated list and also it have to trigger another event after i selected an item...
Thanks in advance..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,看起来您正在将 id 选择器
#tags
与类选择器.tags
混合在一起。您需要一致地引用该对象(全部应该是
#tags
)。这应该可以解决你的问题。另外,请考虑对建议列表中出现的对象使用 jQuery 的
.live()
方法,为它们分配点击处理程序。First off, looks like you are mixing an id selector
#tags
with a class selector.tags
.You need to refer to the object consistently (all should be
#tags
). This should solve your problem.Also, consider using jQuery's
.live()
method for the objects that appear in the suggestion list to assign a click handler to them.我认为您应该使用“select”事件,并访问所选值的 ui.item.label:
问候。
I think you should use the "select" event, and access the ui.item.label for the selected value:
Regards.