使用javascript自动完成功能显示的城市名称
码:
$.ajax({
url : '/Addbus',
type : 'POST',
dataType : 'JSON',
success : function(data){
var dt = JSON.parse(data.body);
$.each(dt,function(i,v){
$.each(v,function(j,p){
var arr={};
arr= p.name+ ',' +p.id;
temp.push(arr);
var result=JSON.stringify(temp);
console.log(result);
$("#source").autocomplete({
source: result, // The source of the AJAX results
minLength: 3, // The minimum amount of characters that must be typed before the autocomplete is triggered
focus: function( event, ui ) { // What happens when an autocomplete result is focused on
$("#source").val( ui.item.value );
return false;
},
select: function ( event, ui ) { // What happens when an autocomplete result is selected
$("#source").val( ui.item.value );
$('#source-id').val( ui.item.label );
}
});
当我在文本框中选择cityname
然后在ui.item.value
和ui.item.label
中我得到的值为item:label:“Hyderabad,6”value:“Hyderabad,6”,现在我如何能够分离它们并采用不同的值。示例我希望值为id = 6和name = hyderabad。
也许这会对你有所帮助,不要对结果数组进行字符串化,在源代码中将其用作数组
$.ajax({
url : '/Addbus',
type : 'POST',
dataType : 'JSON',
success : function(data){
var dt = JSON.parse(data.body);
$.each(dt,function(i,v){
$.each(v,function(j,p){
var arr={};
arr.label = p.name;
arr.value = p.id;
temp.push(arr);
var result=temp
$("#source").autocomplete({
source: result, // The source of the AJAX results
minLength: 3, // The minimum amount of characters that must be typed before the autocomplete is triggered
focus: function( event, ui ) { // What happens when an autocomplete result is focused on
$("#source").val( ui.item.value );
return false;
},
select: function ( event, ui ) { // What happens when an autocomplete result is selected
$("#source").val( ui.item.value );
$('#source-id').val( ui.item.label );
}
});
使用javascript自动完成功能显示的城市名称
码:
$.ajax({
url : '/Addbus',
type : 'POST',
dataType : 'JSON',
success : function(data){
var dt = JSON.parse(data.body);
$.each(dt,function(i,v){
$.each(v,function(j,p){
var arr={};
arr= p.name+ ',' +p.id;
temp.push(arr);
var result=JSON.stringify(temp);
console.log(result);
$("#source").autocomplete({
source: result, // The source of the AJAX results
minLength: 3, // The minimum amount of characters that must be typed before the autocomplete is triggered
focus: function( event, ui ) { // What happens when an autocomplete result is focused on
$("#source").val( ui.item.value );
return false;
},
select: function ( event, ui ) { // What happens when an autocomplete result is selected
$("#source").val( ui.item.value );
$('#source-id').val( ui.item.label );
}
});
当我在文本框中选择cityname
然后在ui.item.value
和ui.item.label
中我得到的值为item:label:“Hyderabad,6”value:“Hyderabad,6”,现在我如何能够分离它们并采用不同的值。示例我希望值为id = 6和name = hyderabad。
也许这会对你有所帮助,不要对结果数组进行字符串化,在源代码中将其用作数组
$.ajax({
url : '/Addbus',
type : 'POST',
dataType : 'JSON',
success : function(data){
var dt = JSON.parse(data.body);
$.each(dt,function(i,v){
$.each(v,function(j,p){
var arr={};
arr.label = p.name;
arr.value = p.id;
temp.push(arr);
var result=temp
$("#source").autocomplete({
source: result, // The source of the AJAX results
minLength: 3, // The minimum amount of characters that must be typed before the autocomplete is triggered
focus: function( event, ui ) { // What happens when an autocomplete result is focused on
$("#source").val( ui.item.value );
return false;
},
select: function ( event, ui ) { // What happens when an autocomplete result is selected
$("#source").val( ui.item.value );
$('#source-id').val( ui.item.label );
}
});