简体   繁体   English

在自动完成jQuery中更改事件

[英]Change event in Autocomplete jQuery

in my project im using the jQuery Autocomplete which is working great. 在我的项目中,我使用的是jQuery Autocomplete,效果很好。 the problem is, that i want to handle a situation were a user changes the input of the text. 问题是,我想处理一种情况,即用户更改了文本输入。 for ex. 对于前。 the data contains ("prov 1", "prov 2"), the user choose from the menu "prov 1" but then changed it to "prov 2" by changing the 1 with 2. i implemented the "change" event in the js but the ui.item is empty (i guess since the menu is not shown..) i read that the "change" event is fired only when the text box is losing focus -> not to good for me, since the flow is selecting a "prov" and clicking button to continue. 数据包含(“省1”,“省2”),用户可以从菜单“省1”中进行选择,然后通过将其2更改为1将其更改为“省2”。 js,但ui.item为空(我想因为菜单没有显示。)我读到“ change”事件仅在文本框失去焦点时才触发->对我不利,因为流程是选择一个“省”,然后单击按钮继续。 any suggestions ?? 有什么建议么 ??

here is my code: JS 这是我的代码:JS

 $(element).autocomplete({
        minLength: 2,
        source: function (request, response) {
            $.ajax({
                url: requestUrl,
                dataType: "json",
                data: { query: request.term },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.Label,
                            value: item.Label,
                            realValue: item.Value
                        };
                    }));
                },
            });
        },
        select: function (event, ui) {
            var hiddenFieldName = $(this).attr('data-value-id');
            $('#' + hiddenFieldName).val(ui.item.realValue);
            hiddenFieldName = $(this).attr('data-value-name');
            $('#' + hiddenFieldName).val(ui.item.label);
        },
        change: function (event, ui) {
            alert('changed');
            if (!ui.item) {                    
                alert('no value'); /* IS FIRED */
            } else {

            }
        }
    });
});

尝试按键事件处理程序-http: //api.jquery.com/keypress/

Try change this: 尝试更改此:

change: function (event, ui) {

to this: 对此:

input: function (event, ui) {

Or you can try with this: 或者,您可以尝试以下操作:

 $(element).autocomplete({
        minLength: 2,
        //all your code but not change: function
 }).on('input', function(e){
     alert('changed');
     if (this.value.length <== 0) {                    
        alert('no value'); /* IS FIRED */
     } else {
        alert(this.value);
     }
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM