简体   繁体   English

jQuery:自动完成后的文本框文本更改事件?

[英]jQuery: Textbox text change event after autocomplete?

I'm using jquery autocomplete plugin for a textbox: 我正在为文本框使用jquery自动完成插件:

$('#TargetArea').autocomplete({
    source: '@Url.Action("GetTarget", "Ads", new { type = "Zip", term = target })'  
    });

It works fine. 工作正常。 Now, I want to do is: when the textbox text changed, call an action to get data from database, then show the data in another div. 现在,我要做的是:当文本框文本更改时,调用一个操作以从数据库获取数据,然后在另一个div中显示数据。

$('#TargetArea').change(function () {
       var url = "/My/Test";
       var target = $("#TargetArea").val();
       $.post(url, { Target: target }, function (data) {
           $("#resultId").html(data);
       });
   })

However, this change event never triggered. 但是,此更改事件从未触发。 If I comment out the autocomplete part, then it works fine. 如果我注释掉自动完成部分,则可以正常工作。 Anyone knows what the problem is? 有人知道是什么问题吗? or, How I should do this? 或者,我该怎么做?

Thanks 谢谢

I think you should use the change event of the autocomplete plugin. 我认为您应该使用自动完成插件的change事件。

See the documentation here: http://api.jqueryui.com/autocomplete/#event-change 请参阅此处的文档: http : //api.jqueryui.com/autocomplete/#event-change

Check it, I think it should works. 检查一下,我认为应该可以。

$( "#TargetArea" ).autocomplete({
  source: '@Url.Action("GetTarget", "Ads", new { type = "Zip", term = target })',  
  change: function( event, ui ) {}
});

you can make this either 你可以做到这一点

1 - Initialize the autocomplete with the change callback specified: 1-使用指定的更改回调初始化自动完成:

    $( '#TargetArea' ).autocomplete({
      source: '@Url.Action("GetTarget", "Ads", new { type = "Zip", term = target })',
      change: function( event, ui ) {
       var url = "/My/Test";
       var target = $("#TargetArea").val();
       $.post(url, { Target: target }, function (data) {
           $("#resultId").html(data);
       });
      }
    });

or 要么

2- Bind an event listener to the autocompletechange event: 2-将事件侦听器绑定到autocompletechange事件:

$('#TargetArea').autocomplete({
    source: '@Url.Action("GetTarget", "Ads", new { type = "Zip", term = target })'  
    });

    $( '#TargetArea' ).on( "autocompletechange", function( event, ui ) {
                            var url = "/My/Test";
                            var target = $("#TargetArea").val();
                            $.post(url, { Target: target }, function (data) {
                                    $("#resultId").html(data);
                                   });
                            });

This will be triggered when the field is blurred, if the value has changed. 如果值已更改,则在字段模糊时将触发此操作。

Source : http://api.jqueryui.com/autocomplete/#event-change 来源: http : //api.jqueryui.com/autocomplete/#event-change

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

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