将值设置为jquery自动完成组合框 [英] set value to jquery autocomplete combobox

查看:95
本文介绍了将值设置为jquery自动完成组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jquery autocomplete combobox
,一切正常。但我也希望通过JavaScript设置特定值,如 $(#value)。val(somevalue)并设置为select元素,但输入没有变化具有自动完成功能的元素。

I am using jquery autocomplete combobox and everything is ok. But I also want to set specific value through JavaScript like $("#value").val("somevalue") and it set to select element, but no changes in input element with autocomplete.

当然,我可以直接选择此输入并设置值,但还有其他一些方法吗?我尝试将绑定绑定到 this.element ,如 this.element.bind(change,function(){alert(1)})但没有效果。我不知道为什么。

Of course, I can select this input and set value directly, but is it some other ways to do that? I try set bind to this.element like this.element.bind("change", function(){alert(1)}) but it was no effects. And I don't know why.

编辑

我找到了解决此案例的方法。但我不喜欢它。我已经为ui.combobox的_create函数添加了以下代码

I found a workaround for this case. But I don't like it. I have added the following code to _create function for ui.combobox

this.element.bind("change", function() {  
    input.val( $(select).find("option:selected").text());  
});

当我需要更改值时,我可以使用 $(#选择器)。val(specificvalue)。触发器(更改);

And when I need to change the value I can use $("#selector").val("specificvalue").trigger("change");

推荐答案

此演示 您要找的是什么?

Is this demo what you are looking for?

该链接将jQuery UI自动完成的值设置为 Java 。焦点留在输入上,以便可以使用普通的键盘事件来导航选项。

The link sets the value of the jQuery UI autocomplete to Java. The focus is left on the input so that the normal keyboard events can be used to navigate the options.

编辑如何添加其他功能像这样的组合框:

How about adding another function to the combobox like this:

autocomplete : function(value) {
    this.element.val(value);
    this.input.val(value);
}

并使用您想要设置的值调用它:

and calling it with the value you want to set:

$('#combobox').combobox('autocomplete', 'Java'); 

更新了演示

我找不到任何可用的现有功能来做你想做的事情,但这看起来效果很好为了我。希望它更接近你需要的行为。

I cannot find any available existing function to do what you want, but this seems to work nicely for me. Hope it is closer to the behaviour you require.

这篇关于将值设置为jquery自动完成组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆