jquery UI Combobox ONCHANGE

前端 未结 8 970
死守一世寂寞
死守一世寂寞 2020-12-02 13:28

how can I attach an onchange function in a jqueryUI combobox? Here is my code:

$(\".cmbBox\").combobox({
     change:function(){
         alert($(this).val()         


        
8条回答
  •  萌比男神i
    2020-12-02 14:02

    Into the ui.combobox plugin :

    i added at the end of the select method :

    $(input).trigger("change", ui);
    

    i added before the "var input ..." :

    select.attr('inputId', select.attr('id') + '_input');
    

    after, to have a functional onchange event... on ui.combobox i commented the change method and moved its code to the checkval method that extends ui.autocomplete :

    $.extend( $.ui.autocomplete, {
        checkVal: function (select) {
                var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"),
                            valid = false;
                $(select).children("option").each(function () {
                    if ($(this).text().match(matcher)) {
                        this.selected = valid = true;
                        return false;
                    }
                });
                if (!valid) {
                    // remove invalid value, as it didn't match anything
                    $(this).val("");
                    $(select).val("");
                    $(this).data("autocomplete").term = "";
                    $(this).data("autocomplete").close();
                }
            }
    });
    

    and i coded the input change method as below :

    var oCbo = ('#MyCbo').combobox();
    $('#' + oCbo.attr('inputId')).change(function () {
    
        // from select event : check if value exists
        if (arguments.length < 2) {
            $.ui.autocomplete.checkVal.apply(this, [oCbo]);
        }
    
            // YOUR CODE HERE
    });
    

提交回复
热议问题