使用Ajax自动完成jQuery UI
我成功获得了搜索响应;但是,我需要通过将响应中的值传递给JavaScript来对选定结果执行操作。
这是我的JSON和我的代码段:
[{
"id": 10,
"value": "Amy",
"country": "Algeria"
},
{
"id": 37,
"value": "Amon",
"country": "Belgium"
}]
function Searchme() {
var search_value = jQuery("#searchboxrecord").val();
var search_url = "{{ path('search', {'value': 'text'}) }}";
search_url = search_url.replace('text', search_value);
$('#searchboxrecord').autocomplete({
source: function (request, response) {
$.ajax({
type: "GET",
url: search_url,
data: {},
dataType: "json",
success: function (data) {
response(data);
}
});
},
minLength: 1,
select: function (event, ui) {
$('.myText .show-content[data-type= "' + country + '"] ').fadeIn('slow');
}
});
}
答案 0 :(得分:0)
我只是注意到我可以使用ui.item
function Searchme(){
var search_value = jQuery ("#searchboxrecord").val();
var search_url = "{{ path('search', {'value': 'text'}) }}";
search_url = search_url.replace('text', search_value);
$('#searchboxrecord').autocomplete({
source: function (request, response) {
$.ajax({
type: "GET",
url: search_url,
data: {},
dataType: "json",
success: function(data){
response(data);
}
});
},
minLength: 1,
select: function (event, ui) {
var country = ui.item.country;
$('.myText .show-content[data-type= "' + country + '"] ').fadeIn('slow');
$('.myText .show-content[data-type!= "' + country + '"] ').fadeOut('slow');
}
});
}