How do you set a default value with jquery auto complete combobox?

后端 未结 14 1974
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-28 19:01

When using the jquery ui autocomplete combobox, can you set a default value for the combobox?

14条回答
  •  感动是毒
    2020-12-28 19:22

    I stumped this error and cannot fix it OnLoad event whatever I coded (Probably on 3 hours). At last luckily I acrossed http://www.onemoretake.com/2011/04/17/a-better-jquery-ui-combo-box/ web page(Thx for @Dan for solving my headache) and saw the real missing part is not on "OnLoad", on ui definition function itself. Standart definition function on the official Jquery Ui web page does not contain programmatically select option.

    Here is the function should added to definition function :

    //allows programmatic selection of combo using the option value
            setValue: function (value) {
                var $input = this.input;
                $("option", this.element).each(function () {
                    if ($(this).val() == value) {
                        this.selected = true;
                        $input.val(this.text);
                        return false;
                    }
                });
            }
    

    also i produced another function to change selection via option text not option value

    //allows programmatic selection of combo using the option text
                setValueText: function (valueText) {
                    var $input = this.input;
                    var selectedText;                   
                    $("option", this.element).each(function () {
                        if ($(this).text() == valueText) {
                            this.selected = true;
                            $input.val(this.text);                                                    
                            return false;
                        }
                    });                        
                }
    

    You can use these functions on OnLoad or another function as :

    $("#yourComboBoxID").combobox("setValue", "Your Option Value");
    

    or

    $("#yourComboBoxID").combobox("setValueText", "Your Option Text");
    

提交回复
热议问题