我有这种自动填充方法:
$("#year").autocomplete({
source: "http://localhost/ajax_autocomplete.php?type=year",
minLength: 2,
select: function(event, ui) {
var year = ui.item.value; // need to pass year to next autocomplete
},
});
我需要使用上面代码中的选定年份作为第二个自动填充方法中的查询字符串:
$("#price").autocomplete({
source: "http://localhost/ajax_autocomplete.php?type=price&year=" + year,
minLength: 2,
select: function(event, ui) {
// do stuff...
},
});
我怎样才能做到这一点?我尝试在localStorage
中设置值,但这不起作用。
答案 0 :(得分:2)
我认为你需要这个......
$(document).on('focus','.type',function(){
type = $(this).data('type');
if(type =='year' )autoTypeNo = 0;
if(type =='price' )autoTypeNo = 1;
$(this).autocomplete({
source: function( request, response ) {
$.ajax({
url : 'autocomplete.php',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
type: type
},
success: function( data ) {
response( $.map( data, function( item ) {
var code = item.split("|");
return {
label: code[autoTypeNo],
value: code[autoTypeNo],
data : item
}
});
}
});
},
autoFocus: true,
minLength: 0,
select: function( event, ui ) {
var names = ui.item.data.split("|");
$('#year').val(names[0]);
$('#price').val(names[1])
}
});
});