jQuery UI的自动完成组合框 - 改变不费一枪 [英] Jquery ui Autocomplete Combobox - changed not firing

查看:158
本文介绍了jQuery UI的自动完成组合框 - 改变不费一枪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用jQuery UI自动完成组合框帮助。我不能得到的更改事件的火不管。我从不同的例子拉到一起这个小程序。

我有一个的jsfiddle这里当前工作的例子。我希望它ALERT我已更改选择ID(我有这些小部件的4我的网站建设上的一个页面上)。

我没有得到任何错误的JS从这个例子中,所以我究竟做错了什么?在此先感谢

 (函数($){
    $ .widget(custom.combobox,{        _create:功能(){
            this.wrapper = $(&所述;跨度>中)
                .addClass(自定义组合框)
                .insertAfter(this.element);
            this.element.hide();
            this._createAutocomplete();
            this._createShowAllButton();
        },        _createAutocomplete:功能(){
            VAR选择= this.element.children(:选择),
                值= selected.val()? selected.text():;            this.input = $(<输入>中)
                .appendTo(this.wrapper)
                .VAL(值)
                .attr(称号,)
                .addClass(自定义组合框中输入UI的小部件的UI控件内容的UI状态默认UI的角落遗)
                .autocomplete({
                延迟:0,
                的minLength:0,
                来源:$ .proxy(这一点,_source)
            })
                .tooltip({
                tooltipClass:UI状态,突出
            })
                。对('鼠标松开',函数(){
                $(本)。选择();
            });            $(本).blur();            this._on(this.input,{
                autocompleteselect:函数(事件,UI){
                    ui.item.option.selected = TRUE;
                    this._trigger(选择,事件{
                        项目:ui.item.option
                    });
                },                autocompletechange:_removeIfInvalid
            });
        },        _createShowAllButton:功能(){
            VAR输入= this.input,
                wasOpen = FALSE;            $(&所述a取代;)
                .attr(的tabIndex,-1)
                .attr(称号,显示所有项目)
                .tooltip()
                .appendTo(this.wrapper)
                .button({
                图标:{
                    初级:UI图标三角-1-S
                },
                文本:假
            })
                .removeClass(UI角所有)
                .addClass(自定义组合框,切换UI的角落权)
                .mousedown(函数(){
                wasOpen = input.autocomplete(小部件)是(:可见);
            })
                。点击(函数(){
                input.focus();
                input.blur();                //关闭,如果已经显现
                如果(wasOpen){
                    返回;
                };                //把空字符串值,搜索,显示所有结果
                input.autocomplete(搜索,);
            });
        },        _source:功能(请求,响应){
            VAR匹配=新的RegExp($ ui.autocomplete.escapeRegex(request.term),I);
            响应(this.element.children(选项)。图(函数(){
                变种文字= $(本)的.text();
                如果(THIS.VALUE&安培;及(request.term || matcher.test(文本))!){回报
                    标签:文字,
                    值:文本,
                    选项​​:此
                };
            }));
        },        _removeIfInvalid:函数(事件,UI){
            //选择一个项目,无关
            如果(ui.item){                VAR选择= this.element;
                返回;
            };            //搜索匹配(不区分大小写)
            变种default_text =;            VAR值= this.input.val()
                valueLowerCase = value.toLowerCase(),
                有效= FALSE;            this.element.children(选项)。每个(函数(){                如果($(本).VAL()==默认){
                    default_text = $(本)的.text();
                };                如果($(本)的.text()。与toLowerCase()=== valueLowerCase){
                    this.selected =有效= TRUE;
                    返回false;
                };
            });            //找到一个匹配,无关
            如果(有效){
                返回;
            };            //删除无效值
            this.input.val(default_text)
                .attr(称号,值+没有匹配任何项目)
                .tooltip(开放);            this._delay(函数(){
                。this.input.tooltip(关闭)ATTR(称号,);
            },2500);
            this.input.data(UI-自动完成)一词=。
        },        _destroy:功能(){
            this.wrapper.remove();
            this.element.show();
        },        刷新:功能(){
            选择= this.element.children(:选择);
            this.input.val(selected.text());
        },        选择:函数(事件,UI){
            ui.item.option.selected = TRUE;
            self._trigger(选中,事件,{
                项目:ui.item.option
            });
            select.trigger(变);
        },        改变:函数(事件,UI){
            如果(!ui.item){
                VAR匹配=新的RegExp(^+ $ .ui.autocomplete.escapeRegex($(本).VAL())+$,I),
                    有效= FALSE;
                select.children(选项)。每个(函数(){
                    如果($(本)的.text()。匹配(匹配)){
                        this.selected =有效= TRUE;
                        返回false;
                    };
                });
                如果(!有效){
                    //删除无效的价值,因为它没有匹配
                    $(本).VAL();
                    select.val();
                    。input.data(自动完成)一词=;
                    返回false;
                };
            };
        }
    });
})(jQuery的);


解决方案

简单的方法来检测用户改变了组合框试试这个:

  $(#组合框)。组合框({
    选择:函数(事件,UI){
        警报(THIS.VALUE);
    }
});

I need help with JQuery UI Autocomplete Combobox. I cannot get the "CHANGED" event to fire no matter what. I have pulled together this widget from various examples.

I have a JSFiddle here for the current working example. I want it to "ALERT" me with the select ID that was changed (I have 4 of these widgets on one page on the site I am building).

I get no JS errors from the example, so what am I doing wrong? Thanks in advance

(function ($) {
    $.widget("custom.combobox", {

        _create: function () {
            this.wrapper = $("<span>")
                .addClass("custom-combobox")
                .insertAfter(this.element);
            this.element.hide();
            this._createAutocomplete();
            this._createShowAllButton();
        },

        _createAutocomplete: function () {
            var selected = this.element.children(":selected"),
                value = selected.val() ? selected.text() : "";

            this.input = $("<input>")
                .appendTo(this.wrapper)
                .val(value)
                .attr("title", "")
                .addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left")
                .autocomplete({
                delay: 0,
                minLength: 0,
                source: $.proxy(this, "_source")
            })
                .tooltip({
                tooltipClass: "ui-state-highlight"
            })
                .on('mouseup', function () {
                $(this).select();
            });

            $(this).blur();

            this._on(this.input, {
                autocompleteselect: function (event, ui) {
                    ui.item.option.selected = true;
                    this._trigger("select", event, {
                        item: ui.item.option
                    });
                },

                autocompletechange: "_removeIfInvalid"
            });
        },

        _createShowAllButton: function () {
            var input = this.input,
                wasOpen = false;

            $("<a>")
                .attr("tabIndex", -1)
                .attr("title", "Show All Items")
                .tooltip()
                .appendTo(this.wrapper)
                .button({
                icons: {
                    primary: "ui-icon-triangle-1-s"
                },
                text: false
            })
                .removeClass("ui-corner-all")
                .addClass("custom-combobox-toggle ui-corner-right")
                .mousedown(function () {
                wasOpen = input.autocomplete("widget").is(":visible");
            })
                .click(function () {
                input.focus();
                input.blur();

                // Close if already visible
                if (wasOpen) {
                    return;
                };

                // Pass empty string as value to search for, displaying all results
                input.autocomplete("search", "");
            });
        },

        _source: function (request, response) {
            var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
            response(this.element.children("option").map(function () {
                var text = $(this).text();
                if (this.value && (!request.term || matcher.test(text))) return {
                    label: text,
                    value: text,
                    option: this
                };
            }));
        },

        _removeIfInvalid: function (event, ui) {
            // Selected an item, nothing to do
            if (ui.item) {

                var selected = this.element;
                return;
            };

            // Search for a match (case-insensitive)
            var default_text = "";

            var value = this.input.val(),
                valueLowerCase = value.toLowerCase(),
                valid = false;

            this.element.children("option").each(function () {

                if ($(this).val() == "default") {
                    default_text = $(this).text();
                };

                if ($(this).text().toLowerCase() === valueLowerCase) {
                    this.selected = valid = true;
                    return false;
                };
            });

            // Found a match, nothing to do
            if (valid) {
                return;
            };

            // Remove invalid value
            this.input.val(default_text)
                .attr("title", value + " didn't match any item")
                .tooltip("open");

            this._delay(function () {
                this.input.tooltip("close").attr("title", "");
            }, 2500);
            this.input.data("ui-autocomplete").term = "";
        },

        _destroy: function () {
            this.wrapper.remove();
            this.element.show();
        },

        refresh: function () {
            selected = this.element.children(":selected");
            this.input.val(selected.text());
        },

        select: function (event, ui) {
            ui.item.option.selected = true;
            self._trigger("selected", event, {
                item: ui.item.option
            });
            select.trigger("change");
        },

        change: function (event, ui) {
            if (!ui.item) {
                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("");
                    input.data("autocomplete").term = "";
                    return false;
                };
            };
        }
    });
})(jQuery);

解决方案

simpler way to detect the user has changed the combobox Try this:

$("#combobox").combobox({ 
    select: function (event, ui) { 
        alert(this.value);
    } 
});

这篇关于jQuery UI的自动完成组合框 - 改变不费一枪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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