问题
I am trying to do Autocomplete and onchange event at a time.
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.
My HTML Code
<div style="width: 34%">
Person Name:<input id="txt0" type="text" onchange="SaveData('txt0')" class="userSearch" placeholder="Helped Person" />
</div>
JavaScript Code
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.
Please Help
Thanks in advance.
回答1:
You can use the change event on the autocomplete widget to achieve this. 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(); }
});
}
回答2:
$('#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/
回答3:
$('.userSearch').trigger('change');
That will trigger "change" event
回答4:
KDRVN is correct.
$( ".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)"
来源:https://stackoverflow.com/questions/13601483/fire-onchange-event-after-autocomplete