I am trying to fire a change event after auto complete.this change event will load a textbox with a name corresponding auto completes selected id. auto complete event is working perfectly but the change event is not responding.
<script>
$('#CustomerSupplyId').autocomplete({
source: function (request, response) {
$.ajax({
type: "GET",
url: "/Purchase/GetSuggestion",
data: { text: request.term },
success: function (data) {
response($.map(data, function (profile) {
return { label: profile.CustomerSupplyNm, value: profile.CustomerSupplyId, id: profile.CustomerSupplyId }
}
))
},
change: function (data) {
response($.map(data, function (profile) {
$("#textbox").val(profile.CustomerSupplyNm);
}))
}
})
}
});
</script>
What am i doing wrong here or what should i do?
if i do the success method like this
success: function (data) {
response($.map(data, function (profile) {
return { label: profile.CustomerSupplyNm, value: profile.CustomerSupplyId, id: profile.CustomerSupplyId },
$("#textbox").val(profile.CustomerSupplyNm)
}
))
}
the 'testbox' is loaded but the auto complete suggestion is despaired.But the 'textbox value is being changed for every keystroke which i don't want.
Set the textbox value in response's callback before returning
$('#CustomerSupplyId').autocomplete({
source: function (request, response) {
$.ajax({
type: "GET",
url: "/Purchase/GetSuggestion",
data: { text: request.term },
success: function (data) {
response($.map(data, function (profile) {
$("#textbox").val(profile.CustomerSupplyNm);
return { label: profile.CustomerSupplyNm, value: profile.CustomerSupplyId, id: profile.CustomerSupplyId }
}))
}
})
}
});
there is no change attribute for jquery's .ajax() method,
check docs here .
you should listen events after ajax call has been completed like:
$.ajax({
type: "GET",
url: "/Purchase/GetSuggestion",
data: { text: request.term },
success: function (data) {
response($.map(data, function (profile) {
return { label: profile.CustomerSupplyNm, value: profile.CustomerSupplyId, id: profile.CustomerSupplyId }
}
))
},
complete: function (data) {
do your dom manipulation here
}
})
jQuery ajax doesn't have a property change. So adding change property doesn't help.
I think what you want to do is something like this.
...
.autocomplete({
source: {
...
}
change: {
// Do something ...
}
})
...
Related
I am getting response through autofill which i can view through inspect in chrome. But I couldn't display properly neither i am getting any result in console.log
$(document).ready(function(){
$(function() {
$("#search_text").autocomplete({
source: "/find_city",
minLength: 1,
data:$('#search_text').val(),
success:function(data){
console.log(data);
}
});
});
})
Your source must be your ajax request
source: function (request, response) {
$.ajax({
url: "/find_city",
data: $('#search_text').val(),
dataType: "json",
success: function (data) {
// do something
}
});
}
and in your callback just return assign your desired value and label for the select input option element(s)
response($.map(data, function (item) {
return {
label: item.name,
value: item.name
}
}));
So you code would be like this
source: function (request, response) {
$.ajax({
url: "/find_city",
data: $('#search_text').val(),
dataType: "json",
success: function (data) {
response($.map(data, function (item) {
return {
label: item.name,
value: item.name
}
}));
}
});
}
I have found solution to my issue. I have changed name field from cities table to value and it works like a charm. I could also use key field
i used jqueryui autocomplete iwant be the same work for some input text
but when i use $(this).val() for send value of textbox it show error and when i use (".Degree").val() shows and send to server first textbox it incorrect
function DegreeAutoComplete() {
$(".Degree").autocomplete({
position: { my: "right top", at: "right bottom", collision: "none" },
source: function (request, response) {
var spin = $(".spinnerDegree");
spin.addClass("fa-spin fa-spinner");
$.ajax({
url: "#Url.Action("GetDegree")",
type: "POST",
dataType: "json",
data: { search: $(".Degree").val() },
success: function (data) {
spin.removeClass("fa-spin fa-spinner");
response($.map(data, function (item) {
return { label: item.PersianName, value: item.PersianName, id: item.Id };
}));
}
});
},
messages: {
noResults: '',
results: function (resultsCount) { }
},
select: function (event, ui) {
// ui.item.value contains the id of the selected label
alert($(this).val());
$(this).attr("sel", ui.item.id);
}
});
}
when i use: $(this).val();
elem.nodeName is undefined
hooks = jQuery.valHooks[ elem.type ] || jQuery.valHooks[elem.nodeName.toLowerCa...
how can i fix it
this refers to ajax object inside it. Save this reference before using it in inside scope.
var spin = $(".spinnerDegree");
spin.addClass("fa-spin fa-spinner");
var $that = $(this);// save reference
$.ajax({
url: "#Url.Action("
GetDegree ")",
type: "POST",
dataType: "json",
data: { search: $that.val() },//use $that var here
success: function(data) {
spin.removeClass("fa-spin fa-spinner");
response($.map(data, function(item) {
return { label: item.PersianName, value: item.PersianName, id: item.Id };
}));
}
});
EDIT:
Just checked jquery UI doc
You should use request.term thats it.
source: function(request, response) {
$.ajax({
url: "#Url.Action("GetDegree")",
type: "POST",
dataType: "json",
data: {
search: request.term
},
success: function(data) {
//....
}
});
}
i am Creating a page that search all client details...
i am also using json for autocomplete textbox with data of datatable...
<script type="text/javascript">
$(document).ready(function () {
$("#<%=txtSearch.ClientID %>").autocomplete({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/Service.asmx/GetCustomers") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[0],
val: item.split('-')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
select: function (e, i) {
$("#<%=hfCustomerId.ClientID %>").val(i.item.val);
},
minLength: 1
});
});
i want to view data of that selected name on textbox on textchanged event,
their setup of textbgox looks like..`
<asp:TextBox ID="txtSearch" runat="server" AutoPostBack="True"
ontextchanged="txtSearch_TextChanged"></asp:TextBox>
That textchenged event trigger properly in mozilla, but not working in chrome,,
can anyone suggests some solution....
I would suggest calling server side's textbox onchange event after selecting a value from the autocomplete list as,
...
select: function (e, i) {
$("#<%=hfCustomerId.ClientID %>").val(i.item.val);
__doPostBack("txtSearch", "TextChanged");
},
minLength: 1
});
and remove autopostback property of that textbox as we are firing it from client side.
Or you can make an ajax call on select event and populate required data in client side itself.
` $(document).ready(function () {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
InitAutoCompl();
});
function InitializeRequest(sender, args) {
}
function EndRequest(sender, args) {
InitAutoCompl();
}
function InitAutoCompl() {
$("#<%=txtSearch.ClientID %>").autocomplete({
source: function (request, response) {
$.ajax({
url: ''<%=ResolveUrl("~/Service.asmx/GetCustomers") %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('##')[0],
val: item.split('##')[1]
}
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
},
minLength: 1, change: function (event, ui) { __doPostBack("txtSearch", "TextChanged"); }
});
}
and remove Autopostback="true" from TextBox
`
I've written an auto-complete code using jQuery in script tags in a given file.
In that I have various events like select open close etc.
I want to call another function in the same file but not within the jQuery block. However this is not getting called.
$(function() {
function log( message ) {
makeRequest(message);
}
$("#searchArea").autocomplete({
source: function (request, response) {
$.ajax({
type: "GET",
dataType: "jsonp",
jsonp: "callback",
jsonpCallback: "YAHOO.Finance.SymbolSuggest.ssCallback",
data: {
query: request.term
},
cache: true,
url: "http://autoc.finance.yahoo.com/autoc"
});
YAHOO.Finance.SymbolSuggest.ssCallback = function (data) {
response($.map(data.ResultSet.Result, function (item) {
return {
label: item.symbol+', '+item.name+'('+item.exchDisp+')',
value: item.symbol
}
}));
}
},
minLength: 1,
select: function (event, ui) {
$("#searchArea").val(ui.item.symbol);
sendAcURL = "http://cs-server.usc.edu:29686/examples/servlet/HelloWorldExample?symbol="+ui.item.value;
},
open: function () {
$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
},
close: function () {
$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
log(sendAcURL);
}
});
});
function makeRequest(url) {
alert("here");
}
I want to call makeRequest() through select event, but alert does not pop-up, hence my request is not called. Why is that so?
I am using Jquery autocomplete with ajax.
Everything works fine except when I set minLength to 0.
When I choose an element from the list, the autocomplete doesn't stop running and shows me the list again.
Here is the code:
$("#id_from").autocomplete({
source: function (request, response) {
$.ajax({
url: "http://127.0.0.1:8000/core/get_from_cities",
dataType: "json",
data: {term: request.term},
success: function (data) {
response($.map(data.data, function (item) {
return {
label: item.value,
value: item.value
}
}));
}
});
},
minLength: 0
});
Thanks..
EDIT:
Here is the solution I came up with:
$("#id_to").focus(function() {
$( "#id_to" ).autocomplete( "enable" );
});
$("#id_ffrom").autocomplete({
source: function (request, response) {
$.ajax({
url: "http://127.0.0.1:8000/core/get_from_cities",
dataType: "json",
data: {term: request.term},
success: function (data) {
response($.map(data.data, function (item) {
return {
label: item.city,
value: item.city,
}
}));
}
});
},
minLength: 0,
select: function (event, ui) {
$('#id_from_country').val(ui.item.country)
$("#id_ffrom").autocomplete( "disable" );
}
});