gpt4 book ai didi

javascript - 使用 javascript 自动完成显示的城市名称

转载 作者:行者123 更新时间:2023-11-30 14:52:15 26 4
gpt4 key购买 nike

代码:

 $.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.valueui.item.label 我得到值作为元素:标签:“海得拉巴,6”值(value):“海得拉巴,6”,现在我怎样才能将它们分开并接受不同的值。例如,我希望值为 id=6 和 name=hyderabad。

最佳答案

const name = ui.item.value.split(',')[0];
const id = ui.item.value.split(',')[1];

关于javascript - 使用 javascript 自动完成显示的城市名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47936905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com