Answer the question
In order to leave comments, you need to log in
How to solve problem with Jquery Autocomplete when filling multiple fields?
You need to make a script autocomplete the input fields when the user enters an address.
To do this, I wrote a php script that, using the REST API of the Ahunter.ru service ( ahunter.ru/site/doc/api/suggest/address ), collects hints for the address entered by the user, processes them and forms an array in JSON format, for its subsequent processing by Jquery Autocomplete
Problems arose while writing a Jquery script for an ajax request to a php script. jquery script code:
<script type="text/javascript">
$('.address').each(function(){
$(this).autocomplete({ source: function(request,response) {
var val = $('.address').val();
$.getJSON('http://doc.army-help.ru/files/1.php?query=' + val, function(data){
var suggestions = []; // массив для хранения результатов
$.each(data, function(key, val) {
suggestions.push(val.value); // добавляем все элементы
});
response(suggestions);
});
}
});
});
</script>
Answer the question
In order to leave comments, you need to log in
In this case, it is wrong to bypass all fields, each is not a helper here.
It will correctly track the data entered in the field and immediately process the data from it
$('body').on('keyup', '.address', function(){
var item = $(this);
item.autocomplete({ source: function(request,response) {
var val = item.val();
$.getJSON('http://doc.army-help.ru/files/1.php?query=' + val, function(data){
var suggestions = []; // массив для хранения результатов
$.each(data, function(key, val) {
suggestions.push(val.value); // добавляем все элементы
});
response(suggestions);
});
}
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question