【发布时间】:2022-01-28 00:33:12
【问题描述】:
如何通过城市名称自动完成搜索获得城市编号?
例如:
纽约:48.94 和 36.92
数组:
<?php
$city=array(
array('New York','48.97','36.92'),
array('Los Angeles','52.75','27.41'),
array('California','34.05','-118.24'),
array('Houston','52.67','31.18'),
array('Philadelphia','47.47','32.94'),
array('Phoenix','49.84','37.41')
);
?>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<form method="post">
<input id="listcity" type="text" name="car">
</form>
<script>
$(function() {
var availableTags = <?php echo json_encode($city,JSON_UNESCAPED_UNICODE); ?>;
$( "#listcity" ).autocomplete({
source: availableTags
});
});
</script>
谢谢
【问题讨论】:
-
如果你像
array('New York : 48.97 and 36.92'),这样格式化数据会怎样 -
不,数据必须分开。
-
这与
ajax有什么关系?你在使用 jQuery UI 自动完成功能吗?为什么你需要城市“数字”,你不会只自动完成城市名称吗? -
是的,我使用 jQuery UI 自动完成功能。我想通过城市名称访问城市的经纬度。
-
JQuery UI 自动完成有documentation,custom data example 可能适合你
标签: php arrays ajax autocomplete