我正在尝试使用 Javascript 和 php 制作一个简单的自动完成脚本,但它根本不起作用。提前感谢您的帮助!这是我的html。
<!doctype html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https:ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/countries.js"></script>
<meta charset="utf-8">
<title>Countries</title>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="css/jquery-ui.css">
</head>
<body>
<div class="ui-widget">
<label for="country">Country: </label>
<input type="text" name="country" id="country">
</div>
</body>
</html>
这是Javascript
$(function() { // Turn the text input into an Autocomplete widget:
$('#country').autocomplete({
source: 'resources/countries.php',
minLength: 2
});
});
和PHP
<?php // countries.php
header('Content-Type: application/json');
$data = array();
$countries = array( 'Afghaistan', 'Albania', 'Algeria', 'Andorra');
if (isset($_GET['term'])) {
foreach ($countries as $country) {
if (stripos($country, $_GET['term']) !== false) $data[] = $country;
} // End of FOREACH.
} // End of IF.
echo json_encode($data);
再次感谢你们都很棒!