У меня есть livesearch ajax, php, mysql, который состоит из:
json, полученный из php
{
"421": {
"name_1": "First name 1",
"name_2": "Last name 1",
"locality": "Locality 1"
},
"866": {
"name_1": "First name 2",
"name_2": "Last name 2",
"locality": "Locality 2"
},
.....
}и этот html-код:
<input type = "text" name = "customer" id = "customer" class = "form-control input-lg" autocomplete = "off" />
<input type = "text" id = "id_customer" >
<div id = "locality" ></div>
<script>
$(document).ready(function(){
$('#customer').typeahead({
source: function(query, result)
{
$.ajax({
url:"customer_json_inc.php",
method:"POST",
data:{query:query},
dataType:"json",
success:function(data)
{
result($.map(data, function(elem, index){
$('#id_customer').val(index);
console.info(index);
return elem.name_1;
}));
}
})
}
});
});
</script>Демо-версию можно посмотреть здесь
Мне не удается взять один из индексов (например, 421, 866, ...) в зависимости от того, что выбрано на входе id = "customer", и ввести его на входе id = "id_customer", поэтому попробуйте использовать div-id = "locality".
В моем коде результат в строке кода: $('# id_customer').Val(index); вставляет последний индекс, указанный в первой введенной букве, а я хочу вставить индекс выбранного клиента во входные данные.
Спасибо!
когда я ищу имя в #customer из json 'name_1' и выбираю 'First name 1' во вводе #id_customer, чтобы вставить индекс 421



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


не понятно, что именно ты хочешь сделать