如何设置jquery ui组合框选择值或默认文本?

时间:2012-02-26 23:44:28

标签: jquery jquery-ui combobox jquery-ui-autocomplete

我有一个自动完成组合框,其中包含来自mysql的选项,组合框包含供应商的名称和它的id,其中一旦用户从组合框中选择,id将被存储在隐藏字段中,然后存储在会话中。我想知道的是如何根据会话设置自动完成组合框的默认文本/值?

实施例。会话= 25; SupplierText =“Stackoverflow”ID = 25 当用户单击新选项卡时,该页面将自动显示具有基于会话的相应文本的组合框。在这种情况下,组合框应具有Stackoverflow的文本值。只是能够显示文本实际上就足够了。

先生/女士,你的答案会有很大的帮助。

3 个答案:

答案 0 :(得分:2)

我发现在创建组合框时具有“selected”属性的选项将在组合框中被选中。您可以通过销毁组合框来设置所选选项,从选项中删除“selected”属性并将属性添加到要选择的选项,然后再次创建组合框。

这是我的代码:

// Destroy the combobox
$(".combobox").combobox("destroy");

// Unselect the currently selected option
$("#selectlist option:selected").removeAttr("selected");

// Select the option you want to select
$("#selectlist option[value='test']").attr("selected", "selected");

// Create the combobox again
$(".combobox").combobox();

答案 1 :(得分:1)

也许你需要像this

这样的例子

例如:

$("#yourselector").autocomplete({
            source: "your URL",
            minLength: 2,
            select: function(event, ui) {
                $('#selector').val(ui.item.YourDataResponse);
                $('#selector').val(ui.item.YourDataResponse);
            }
});

ui.item默认来自JQuery Ui ..

答案 2 :(得分:0)

如果您只想在创建组合框后设置组合框输入的值,您可以设置选择的值然后...

$(".ui-combobox-input").val($("#select option:selected").text()); 
相关问题