jQuery 自动完成后触发 Onchange 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13601483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:47:33  来源:igfitidea点击:

Fire Onchange event after AutoComplete

jqueryhtmlautocompleteonchange

提问by

I am trying to do Autocomplete and onchange event at a time.

我正在尝试一次执行 Autocomplete 和 onchange 事件。

Means:I want to fire onchange event in a autocomplete textbox. When i write something in the a textbox from keyboard and click outside the textbox then onchange event firing but when i select something in the from the auto suggest names onchange event is not firing.

意思是:我想在自动完成文本框中触发 onchange 事件。当我从键盘在文本框中写一些东西并在文本框外单击然后 onchange 事件触发但是当我从自动建议名称中选择某些内容时 onchange 事件不会触发。

My HTML Code

我的 HTML 代码

<div style="width: 34%">
   Person Name:<input id="txt0" type="text" onchange="SaveData('txt0')" class="userSearch" placeholder="Helped Person" />
</div>

JavaScript Code

JavaScript 代码

function AutoComplete() {
//Autocomplete added to the textbox.
$('.userSearch').autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "CTC.aspx/GetMemberName",
            data: "{ 'prefixText': '" + request.term + "' }",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                response(data.d)
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert('Error occured while autocomplete');
            }
        });
    },
    minLength: 1
});
}

function SaveData(textBoxId) {
     alert(textBoxId);
}

I want to fire the onchange event after selected from autocomplete.

我想在从自动完成中选择后触发 onchange 事件。

Please Help

请帮忙

Thanks in advance.

提前致谢。

采纳答案by kdrvn

You can use the change event on the autocomplete widget to achieve this. http://api.jqueryui.com/autocomplete/#event-change

您可以使用自动完成小部件上的更改事件来实现此目的。 http://api.jqueryui.com/autocomplete/#event-change

For example:

例如:

function AutoComplete() {
  //Autocomplete added to the textbox.
  $('.userSearch').autocomplete({
      source: function (request, response) {
         // your ajax code
      },
      minLength: 1,
      change: function (event, ui) { SaveData(); }
  });
}

回答by Pardeep Saini

$('#1').autocomplete({
    select: function(event, ui) {
        "use strict";
        alert('select event called');
       },
    source: ["ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme"],

    minLength: 1,
    change: function(event, ui) {
        if (ui.item) {
            alert("ui.item.value: " + ui.item.value);
        } else {
            alert("ui.item.value is null");
        }
        console.log("this.value: " + this.value);
    }
});

This will can perform both select and change event for autocomplete.

这将可以执行自动完成的选择和更改事件。

you can also check fiddle http://jsfiddle.net/74wLyd8v/

你也可以检查小提琴http://jsfiddle.net/74wLyd8v/

回答by goFrendiAsgard

$('.userSearch').trigger('change');

That will trigger "change" event

这将触发“更改”事件

回答by maxweber

KDRVN is correct.

KDRVN 是正确的。

$( ".selector" ).autocomplete({
  select: function( event, ui ) {}
});

as in

$( "#models" ).autocomplete({source: models,  select: function( event, ui ) {alert('flippy');} });

Not any of these:

这些都不是:

    $('#models').on('select', function() { alert('waawoo'); });
    $('#models').on('change', function() { alert('feefaa'); });
 onchange="updateModels(this)"